<<<<<<< HEAD
=======
 
>>>>>>> develop
#Fine grained Classification
#
#1.Models
#    model_OSME_miru.py -> Attention branch + OSME
#    model_OSME_alpha.py -> OSME alpha
#    model_with_OSME_SE.py -> OSME
#    model_OSME_miru_dog.py -> Attention branch + OSME with StandfordDogs dataset
#    
#2. How to use
#    Just run the python scripts.
#    To change models or other things rewrite the code.
#    
#3.Using ImageDataGenerator.flow_from_directory:
#Example:
        
train_datagen = ImageDataGenerator(rescale = 1./255, 
                                   zoom_range=[0.6,1],
                                   rotation_range=30,
                                   horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale = 1./255)

train_generator = train_datagen.flow_from_directory(
        train_path,
        target_size=(img_size, img_size),
        batch_size=BATCH_SIZE,
        seed = 13,
        multi_outputs=True,  # If the model has multi outputs: True, else False.(default is False)
        out_n = 2 # write the number of outputs that the model has here.
        )

validation_generator = test_datagen.flow_from_directory(
        test_path,
        target_size=(img_size, img_size),
        batch_size=BATCH_SIZE,
        seed = 13,
        multi_outputs=True,  # If the model has multi outputs: True, else False.(default is False)
        out_n = 2 # write the number of outputs that the model has here.
        )
    
#4. Using Crop generator.
# 
# Example:
 
def random_crop(img, random_crop_size,seed):
    # Note: image_data_format is 'channel_last'
    np.random.seed(seed=seed)
    assert img.shape[2] == 3
    height, width = img.shape[0], img.shape[1]
    dy, dx = random_crop_size
    x = np.random.randint(0, width - dx + 1)
    y = np.random.randint(0, height - dy + 1)
    return img[y:(y+dy), x:(x+dx), :]


def crop_generator(batches, crop_length , seed):
    """Take as input a Keras ImageGen (Iterator) and generate random
    crops from the image batches generated by the original iterator.
    """
    while True:
        batch_x, batch_y = next(batches)
        batch_crops = np.zeros((batch_x.shape[0], crop_length, crop_length, 3))
        for i in range(batch_x.shape[0]):
            batch_crops[i] = random_crop(batch_x[i], (crop_length, crop_length),seed)
        yield (batch_crops, batch_y)

seed = 13
crop_size = 392
train_generator_cropped = crop_generator(train_generator, crop_size, seed)
validation_generator_cropped = crop_generator(validation_generator, crop_size, seed)