Exemple #1
0
trainLabelsFile = 'H:/Documents/Datasets/Hands2017/Cropped/trainLabelCropped.csv'
trainImagesFolder = 'H:/Documents/Datasets/Hands2017/Cropped/train/'
testLabelsFile = 'H:/Documents/Datasets/Hands2017/Cropped/testLabelCropped.csv'
testImagesFolder = 'H:/Documents/Datasets/Hands2017/Cropped/test/'
cropSize = 176


def preprocess(image):
    mark = image[0, 0].copy()
    image[0, 0] = (image[0, 1] + image[1, 0]) / 2.
    image = image - mark
    image = image / 255.
    return image


datagen = ImageDataGenerator(preprocessing_function=preprocess)
# validation_split=0.10)
features = [
    "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
    "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22",
    "F23", "F24", "F25", "F26", "F27", "F28", "F29", "F30", "F31", "F32",
    "F33", "F34", "F35", "F36", "F37", "F38", "F39", "F40", "F41", "F42",
    "F43", "F44", "F45", "F46", "F47", "F48", "F49", "F50", "F51", "F52",
    "F53", "F54", "F55", "F56", "F57", "F58", "F59", "F60", "F61", "F62", "F63"
]


def GetGenerators():
    traindf = pd.read_csv(trainLabelsFile, dtype=str)
    # load and iterate training dataset
    train_generator = datagen.flow_from_dataframe(
Exemple #2
0
model = Model(inputs=vgg16_model.input, outputs=preds)

model.summary()

for i, layer in enumerate(model.layers):
    print(i, layer.name)

for layer in model.layers[:19]:
    layer.trainable = False
for layer in model.layers[19:]:
    layer.trainable = True

for i, layer in enumerate(model.layers):
    print(i, " " + layer.name, layer.trainable)

train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
validate_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

train_generator = train_datagen.flow_from_directory(directory=r"./train/",
                                                    target_size=(300, 300),
                                                    color_mode="rgb",
                                                    batch_size=4,
                                                    class_mode="binary",
                                                    shuffle=True,
                                                    seed=42)

validate_generator = validate_datagen.flow_from_directory(
    directory=r"./validate/",
    target_size=(300, 300),
    color_mode="rgb",
    batch_size=4,
log = logging.getLogger(__name__)

log.info("Tensorflow version: %s" % tf.__version__)

img_width = 500
img_height = 500

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

log.info("loading train batches...")
train_batches = ImageDataGenerator().flow_from_directory(
    directory=os.path.join(RESOURCES_DIR, "batches", "train"),
    target_size=(img_width, img_height),
    class_mode="binary",
    batch_size=1)

log.info("loading test batches...")
test_batches = ImageDataGenerator().flow_from_directory(
    directory=os.path.join(RESOURCES_DIR, "batches", "test"),
    target_size=(img_width, img_height),
    class_mode="binary",
    batch_size=1)
print(train_batches.labels)
print(train_batches.filenames)

model = keras.Sequential([
    keras.layers.Flatten(input_shape=input_shape),
    keras.layers.Dense(128, activation='relu'),
Exemple #4
0
        X = it1.next()
        Y = it2.next()
        yield X,Y

# Define our custom metric
def PSNR(y_true, y_pred):
    max_pixel = 1.0
    return 10.0 * (1.0 / math.log(10)) * K.log((max_pixel ** 2) / (K.mean(K.square(y_pred -
y_true))))

# parameters
epochs = int(sys.argv[1])
batch_size = int(sys.argv[2])

# Create generator
datagen = ImageDataGenerator(validation_split=0.1, rescale=1./255)

# Prepare training and validation  datasets
train_it = datagen.flow_from_directory(directory='data/big/', target_size=(200,80), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='training')
val_it = datagen.flow_from_directory(directory='data/big/', target_size=(200,80), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='validation')
train_small_it  = datagen.flow_from_directory(directory='data/small/', target_size=(100,40), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='training')
val_small_it =  datagen.flow_from_directory(directory='data/small/', target_size=(100,40), shuffle=False, color_mode='grayscale', class_mode=None, batch_size=batch_size, subset='validation')

# Build model
input_img = Input(shape=(100, 40, 1))  # adapt this if using `channels_first` image data format
x = UpSampling2D((2, 2), interpolation='bilinear')(input_img)
upsample = Model(input_img, x)

upsample.compile(optimizer='adadelta', loss='mean_squared_error', metrics=[PSNR])

# Train
spc = config_vals['spc']
ep = config_vals['ep']
print("steps per epoch = "+ str(spc))
print("epochs = "+ str(ep))

config_vals['spc'] = spc + 100
config_vals['ep'] = ep + 1

with open("config.yaml", "w") as cw:
   yaml.dump(config_vals, cw, default_flow_style=True)


train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_dataset = train_datagen.flow_from_directory(
        '/covid_folder/xray_dataset_covid19/train/',
        target_size=(64,64),
        batch_size=32,
        class_mode='binary')
test_dataset = test_datagen.flow_from_directory(
        '/covid_folder/xray_dataset_covid19/test/',
        target_size=(64,64),
        batch_size=32,
        class_mode='binary')
working=model.fit(
        train_dataset,
Exemple #6
0
def Learn(augmentation, input_epochs, train_path, val_path, window):
    #path
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    train_dir = os.path.join(BASE_DIR, train_path)
    val_dir = os.path.join(BASE_DIR, val_path)
    print(train_dir)
    print(val_dir)
    # Define hyperparameter
    INPUT_SIZE = 200
    CHANNELS = 3
    INPUT_SHAPE = (INPUT_SIZE, INPUT_SIZE, CHANNELS)
    NUM_CLASSES = window.learn_num_data[0]
    NUM_TRAIN_IMGS = window.learn_num_data[1] * NUM_CLASSES
    NUM_VAL_IMGS = window.learn_num_data[2] * NUM_CLASSES
    BATCH_SIZE = 32

    HORIZONTAL_FLIP = augmentation[0]
    VERTICAL_FLIP = augmentation[1]
    BRIGHTNESS_RANGE = augmentation[2]
    ROTATION_RANGE = augmentation[3]
    if augmentation[4] == True:
        CUT_OUT = cutout(p=0.5,
                         s_l=0.02,
                         s_h=0.4,
                         r_1=0.3,
                         r_2=1 / 0.3,
                         pixel_level=False)
    else:
        CUT_OUT = None

    EPOCHS = input_epochs
    train_steps_per_epoch = NUM_TRAIN_IMGS // BATCH_SIZE
    val_steps_per_epoch = NUM_VAL_IMGS // BATCH_SIZE

    # Data Preprocessing
    training_datagen = ImageDataGenerator(
        rescale=1. / 255,
        horizontal_flip=HORIZONTAL_FLIP,
        vertical_flip=VERTICAL_FLIP,
        brightness_range=BRIGHTNESS_RANGE,
        rotation_range=ROTATION_RANGE,
        preprocessing_function=CUT_OUT,
    )
    validation_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = training_datagen.flow_from_directory(
        train_dir,
        target_size=(INPUT_SIZE, INPUT_SIZE),
        class_mode='categorical',
        batch_size=BATCH_SIZE)

    validation_generator = validation_datagen.flow_from_directory(
        val_dir,
        target_size=(INPUT_SIZE, INPUT_SIZE),
        class_mode='categorical',
        batch_size=BATCH_SIZE)

    # Load pre-trained model
    base_model = tf.keras.applications.VGG16(
        include_top=False,
        weights='imagenet',
        input_shape=INPUT_SHAPE,
    )

    # Freeze the pre-trained layers
    base_model.trainable = False

    # Add a fully connected layer
    model = tf.keras.Sequential()
    model.add(base_model)
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(512, activation='relu'))
    model.add(tf.keras.layers.Dropout(0.5))
    model.add(tf.keras.layers.Dense(256, activation='relu'))
    model.add(tf.keras.layers.Dropout(0.5))
    model.add(tf.keras.layers.Dense(NUM_CLASSES, activation='softmax'))

    model.summary()

    # Compile
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    # Callbacks
    checkpoint_filepath = os.path.join(
        BASE_DIR, 'checkpoint',
        window.settingsData[0] + '_' + window.settingsData[3] + '.h5')

    plotLosses = PlotLosses(input_epochs, window)

    callbacks = [
        tf.keras.callbacks.EarlyStopping(
            patience=10,
            monitor='val_loss',
            #  restore_best_weights=True
        ),
        tf.keras.callbacks.ModelCheckpoint(
            filepath=checkpoint_filepath,
            monitor='val_loss',
            mode='min',
            save_best_only=True,
            # save_weights_only=True,
        ),
        plotLosses,
    ]

    # training model
    history = model.fit(train_generator,
                        epochs=EPOCHS,
                        steps_per_epoch=train_steps_per_epoch,
                        validation_data=validation_generator,
                        validation_steps=val_steps_per_epoch,
                        verbose=1,
                        callbacks=callbacks)
    window.textBox_terminal.append("Training Done!")
    val_loss = history.history['val_loss']
    val_accuracy = history.history['val_accuracy']

    max_val_accuracy = round(np.max(val_accuracy), 4)
    min_val_loss = round(np.min(val_loss), 4)
    message = "Epoch: " + str(np.argmin(val_loss) +
                              1) + " , Min val_loss: " + str(min_val_loss)
    window.textBox_terminal.append(message)
    window.settingsData.append(min_val_loss)
    window.settingsData.append(max_val_accuracy)
    plt.close()
Exemple #7
0
    def predict_on_test(self):
        """
        This function will load the test dataset, pre-process the test
        images and check the performance of the trained models on unseen
        data. This will also save the confusion matrix and classification
        report as csv file in seperate dataframes for each model and for
        each stage, in the evaluation directory.
        
        Arguments:                    
            
            -size_dict    : Contains information about the image input image sizes for each of the models
                
            -model_name   : Name of the model, for example - vgg16, inception_v3, resnet50 etc
                          
            -stage_no     : The training stage of the model. You will have a choice to select the number
                            of training stages. In stage 1, we only fine tune the top 2 dense layers by
                            freezing the convolution base. In stage 2, we will re adjust the weights trained
                            in stage 1 by training the top convolution layers, by freezing the dense layers.
        """

        print("\nStarting model evaluation for stage {}..".format(
            self.stage_no))

        #Create an utility class object to access the class methods
        utils_obj = Utility(self.input_params, self.path_dict)

        df_test = utils_obj.load_data("test")

        test_datagen = ImageDataGenerator(
            preprocessing_function=utils_obj.init_preprocess_func())

        test_generator = test_datagen.flow_from_dataframe(
            dataframe=df_test,
            directory=self.path_dict['source'],
            target_size=utils_obj.init_sizes(),
            x_col="filenames",
            y_col="class_label",
            batch_size=1,
            class_mode='categorical',
            color_mode='rgb',
            shuffle=False)

        nb_test_samples = len(test_generator.classes)

        model = utils_obj.get_models(self.stage_no)
        class_indices = test_generator.class_indices

        def label_class(cat_name):
            return (class_indices[cat_name])

        df_test['true'] = df_test['class_label'].apply(
            lambda x: label_class(str(x)))
        y_true = df_test['true'].values

        #Predictions (Probability Scores and Class labels)
        y_pred_proba = model.predict_generator(test_generator,
                                               nb_test_samples // 1)
        y_pred = np.argmax(y_pred_proba, axis=1)

        df_test['predicted'] = y_pred
        df_test.to_csv(self.path_dict["eval_path"] +
                       "stage{}/".format(self.stage_no) +
                       '{}_predictions_stage_{}.csv'.format(
                           self.input_params['model_name'], self.stage_no))
        dictionary = dict(zip(df_test.true.values, df_test.class_label.values))

        #Confusion Matrixs
        cm = metrics.confusion_matrix(y_true, y_pred)
        df_cm = pd.DataFrame(cm).transpose()
        df_cm = df_cm.rename(mapper=dict,
                             index=dictionary,
                             columns=dictionary,
                             copy=True,
                             inplace=False)
        df_cm.to_csv(self.path_dict["eval_path"] +
                     "stage{}/".format(self.stage_no) +
                     '{}_cm_stage_{}.csv'.format(
                         self.input_params['model_name'], self.stage_no))
        print('Confusion matrix prepared and saved..')

        #Classification Report
        report = metrics.classification_report(y_true,
                                               y_pred,
                                               target_names=list(
                                                   class_indices.keys()),
                                               output_dict=True)

        df_rep = pd.DataFrame(report).transpose()
        df_rep.to_csv(self.path_dict["eval_path"] +
                      "stage{}/".format(self.stage_no) +
                      '{}_class_report_stage_{}.csv'.format(
                          self.input_params['model_name'], self.stage_no))
        print('Classification report prepared and saved..')

        EvalUtils.plot_confusion_matrix(
            self, y_true, y_pred, list(test_generator.class_indices.keys()))

        #General Metrics
        df_metrics = EvalUtils.get_metrics(self, y_true, y_pred)
        df_metrics.to_csv(self.path_dict["eval_path"] +
                          "stage{}/".format(self.stage_no) +
                          '{}_metrics_stage_{}.csv'.format(
                              self.input_params['model_name'], self.stage_no))

        history_df = pd.read_csv(
            self.path_dict["model_path"] + "stage{}/".format(self.stage_no) +
            "{}_history_stage_{}.csv".format(self.input_params['model_name'],
                                             self.stage_no))

        #Get the train vs validation loss for all epochs
        EvalUtils.plt_epoch_error(self, history_df)

        #Generate a complete report and save it as an HTML file in the evaluation folder location
        EvalUtils.get_complete_report(self, y_true, y_pred, class_indices)
Exemple #8
0
def memory_managing():
    print('Using Keras version', keras.__version__)

    train_datagen = ImageDataGenerator(samplewise_center=True,
                                       samplewise_std_normalization=True,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       channel_shift_range=30,
                                       rescale=1. / 255)
    valid_datagen = ImageDataGenerator(samplewise_center=True,
                                       samplewise_std_normalization=True,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       channel_shift_range=30,
                                       rescale=1. / 255)
    test_datagen = ImageDataGenerator(samplewise_center=True,
                                      samplewise_std_normalization=True,
                                      width_shift_range=0.2,
                                      height_shift_range=0.2,
                                      channel_shift_range=30,
                                      rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        directory="food11re/training/",
        target_size=(200, 200),
        color_mode="rgb",
        batch_size=120,
        class_mode="categorical",
        shuffle=True,
        seed=25)
    valid_generator = valid_datagen.flow_from_directory(
        directory="food11re/validation/",
        target_size=(200, 200),
        color_mode="rgb",
        batch_size=120,
        class_mode="categorical",
        shuffle=True,
        seed=25)
    test_generator = test_datagen.flow_from_directory(
        directory="food11re/evaluation/",
        target_size=(200, 200),
        color_mode="rgb",
        batch_size=120,
        class_mode=None,
        shuffle=True,
        seed=25)

    input_size = (200, 200, 3)

    model = cnn_architecture(input_size)

    # model= using_already_trained_models(input_size)
    opt = optimizers.Adam()
    sgd = optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True)
    model.compile(optimizer=opt,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    train_size = train_generator.n // train_generator.batch_size
    valid_size = valid_generator.n // valid_generator.batch_size

    checkpointer = ModelCheckpoint(
        filepath='./first.3.{epoch:02d}-{val_loss:.2f}.hdf5',
        verbose=1,
        save_best_only=True)

    history = model.fit_generator(generator=train_generator,
                                  steps_per_epoch=train_size,
                                  validation_data=valid_generator,
                                  validation_steps=valid_size,
                                  epochs=80,
                                  callbacks=[checkpointer])

    accuracy_loss_plots(history)

    score = model.evaluate_generator(generator=valid_generator,
                                     steps=len(valid_generator))

    print('test loss:', score[0])
    print('test accuracy:', score[1])

    test_generator.reset()

    Y_pred = model.predict_generator(test_generator,
                                     steps=len(test_generator),
                                     verbose=1)
    y_pred = np.argmax(Y_pred, axis=1)

    labels = train_generator.class_indices
    labels = dict((v, k) for k, v in labels.items())
    predictions = [labels[k] for k in y_pred]

    print('Confusion Matrix')
    confusion_m = confusion_matrix(test_generator.classes, y_pred)
    print(confusion_m)
    print('Classification Report')
    target_names = [
        'Bread', 'Dairy_product', 'Dessert', 'Egg', 'Fried_food', 'Meat',
        'Noodles', 'Rice', 'Seafood', 'Soup', 'Veggies'
    ]
    print(
        classification_report(test_generator.classes,
                              y_pred,
                              target_names=target_names))
    plot_confusion_matrix(confusion_m, target_names)

    print("finished")
Exemple #9
0
              metrics=['accuracy'])

if not data_augmentation:
    print('Not using data augmentation.')
    model.fit(x_train,
              y_train,
              batch_size=batch_size,
              epochs=epochs,
              validation_data=(x_val, y_val),
              shuffle=False)
else:
    print('Using real-time data augmentation.')
    datagen = ImageDataGenerator(rotation_range=30,
                                 zoom_range=0.20,
                                 width_shift_range=0.25,
                                 height_shift_range=0.25,
                                 shear_range=0.20,
                                 horizontal_flip=True,
                                 fill_mode="nearest")

    # Fit the model on the batches generated by datagen.flow().
    history = model.fit_generator(datagen.flow(x_train,
                                               y_train,
                                               batch_size=batch_size),
                                  epochs=epochs,
                                  validation_data=(x_val, y_val))

scores = model.evaluate(x_test, y_test)
print(scores)

pd.DataFrame(history.history).plot(figsize=(8, 5))
Exemple #10
0
def train_model(save_mode=False, model_name="NaN"):
    num_classes = 7  #angry, disgust, fear, happy, sad, surprise, neutral
    batch_size = 64
    epochs = 1000

    dataset = fer2013(DATASET_PATH, val_ratio=0.05)
    dataset.reshape_size(IMG_SIZE)

    X_train = dataset.X_train
    Y_train = dataset.Y_train

    X_val = dataset.X_val
    Y_val = dataset.Y_val

    X_test = dataset.X_test
    Y_test = dataset.Y_test

    model = demo_CNN_model(input_size=(IMG_SIZE, IMG_SIZE, 1))

    # generators are used to handle large quanteties of data, important form machine learning
    train_generator = ImageDataGenerator().flow(X_train,
                                                Y_train,
                                                batch_size=batch_size)
    validation_generator = ImageDataGenerator().flow(X_val,
                                                     Y_val,
                                                     batch_size=batch_size)

    early_stopping_monitor = callbacks.EarlyStopping(monitor='val_loss',
                                                     patience=7,
                                                     verbose=1,
                                                     mode='auto')
    checkpointer = callbacks.ModelCheckpoint(
        filepath="saved_models/model_weight.h5",
        verbose=1,
        save_best_only=True)

    sgd = optimizers.SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.fit_generator(generator=train_generator,
                        validation_data=validation_generator,
                        callbacks=[early_stopping_monitor, checkpointer],
                        steps_per_epoch=batch_size,
                        epochs=epochs)

    # ------------- evaluate the model--------------
    train_score = model.evaluate(X_train, Y_train, verbose=0)
    print('Train loss:', train_score[0])
    print('Train accuracy:', 100 * train_score[1])

    test_score = model.evaluate(X_test, Y_test, verbose=0)
    print('Test loss:', test_score[0])
    print('Test accuracy:', 100 * test_score[1])

    # -------------------save model------------------
    if save_mode:
        model_json = model.to_json()
        with open("saved_models/model_graph.json", "w") as json_file:
            json_file.write(model_json)
        # serialize weights to HDF5
        # model.save_weights("saved_models/model_weight.h5")
        print("Saved model to disk")

    return model
Exemple #11
0
    classifier.add(Dropout(0.5))
    classifier.add(Dense(150, activation='relu'))
    #classifier.add(Dropout(0.5))
    classifier.add(Dense(1, activation='linear'))
    #Adding full connection layer increases the parameter size.

    return classifier


# Part 2 - Fitting the CNN to the images

from keras_preprocessing.image import ImageDataGenerator
from PIL import Image

train_datagen = ImageDataGenerator(rescale=1. / 255.,
                                   featurewise_std_normalization=True,
                                   validation_split=0.20)
test_datagen = ImageDataGenerator(rescale=1. / 255.)

training_set = train_datagen.flow_from_dataframe(dataframe=train_dataset,
                                                 directory='./data/training/',
                                                 x_col='image_names',
                                                 y_col='labels',
                                                 subset="training",
                                                 class_mode="other",
                                                 target_size=(480, 480),
                                                 batch_size=30,
                                                 shuffle=True)

validation_set = train_datagen.flow_from_dataframe(
    dataframe=train_dataset,
Exemple #12
0
callbacks = EarlyStopping(monitor='val_loss',
                          patience=1,
                          verbose=1,
                          mode='auto')
# autosave best Model
best_model_file = "data_augmented_weights.h5"
best_model = ModelCheckpoint(best_model_file,
                             monitor='val_acc',
                             verbose=2,
                             save_best_only=True)

# In[16]:

train_datagen = ImageDataGenerator(shear_range=0.1,
                                   zoom_range=0.1,
                                   rotation_range=10.,
                                   width_shift_range=0.1,
                                   height_shift_range=0.1,
                                   horizontal_flip=True)

val_datagen = ImageDataGenerator()

history = vgg16_model.fit_generator(train_datagen.flow(X_train,
                                                       Y_train,
                                                       batch_size=batch_size),
                                    nb_epoch=epochs,
                                    samples_per_epoch=len(X_train),
                                    validation_data=val_datagen.flow(
                                        X_test,
                                        Y_test,
                                        batch_size=64,
                                        shuffle=False),
Exemple #13
0
from tensorflow.python.client import device_lib
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
device_lib.list_local_devices()

# DATA INPUT ------------------------------------------------#
data_dictionary_location = 'C://Users//jbolton//Documents//naughty//deep_tagger//data_dictionary.csv'
images_location = 'C://Users//jbolton//Documents//naughty//deep_tagger//images//'

data_dict_df = pd.read_csv(data_dictionary_location)
label_colnames = list(data_dict_df.columns[1:])

train_data_gen = ImageDataGenerator(
    #    rotation_range = 40
    #,   width_shift_range = 0.2
    #,   height_shift_range = 0.2
    #,   shear_range = 0.2
    #,   zoom_range = 0.2
    #,   horizontal_flip = True
    #,   fill_mode = 'nearest'
)

valid_test_data_gen = ImageDataGenerator()

# here is some code showing an example of what the image augmentation is doing:
my_img = load_img(
    'C://Users//jbolton//Documents//naughty//deep_tagger//images//prod_35.png')
my_img = img_to_array(my_img)
my_img = my_img.reshape((1, ) + my_img.shape)
i = 0
for batch_i in train_data_gen.flow(my_img, batch_size=1):
    array_to_img(batch_i[0]).show()
Exemple #14
0
    def generate_backdoor(
            self, x_val: np.ndarray, y_val: np.ndarray,
            y_target: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        """
        Generates a possible backdoor for the model. Returns the pattern and the mask
        :return: A tuple of the pattern and mask for the model.
        """
        import keras.backend as K
        from keras_preprocessing.image import ImageDataGenerator

        self.reset()
        datagen = ImageDataGenerator()
        gen = datagen.flow(x_val, y_val, batch_size=self.batch_size)
        mask_best: Optional[np.ndarray] = None
        pattern_best: Optional[np.ndarray] = None
        reg_best = float("inf")
        cost_set_counter = 0
        cost_up_counter = 0
        cost_down_counter = 0
        cost_up_flag = False
        cost_down_flag = False
        early_stop_counter = 0
        early_stop_reg_best = reg_best
        mini_batch_size = len(x_val) // self.batch_size
        for _ in tqdm(
                range(self.steps),
                desc=f"Generating backdoor for class {np.argmax(y_target)}"):
            loss_reg_list = []
            loss_acc_list = []

            for _ in range(mini_batch_size):
                x_batch, _ = gen.next()
                y_batch = [y_target] * x_batch.shape[0]
                _, batch_loss_reg, _, batch_loss_acc = self.train(
                    [x_batch, y_batch])

                loss_reg_list.extend(list(batch_loss_reg.flatten()))
                loss_acc_list.extend(list(batch_loss_acc.flatten()))

            avg_loss_reg = np.mean(loss_reg_list)
            avg_loss_acc = np.mean(loss_acc_list)

            # save best mask/pattern so far
            if avg_loss_acc >= self.attack_success_threshold and avg_loss_reg < reg_best:
                mask_best = K.eval(self.mask_tensor)
                pattern_best = K.eval(self.pattern_tensor)
                reg_best = avg_loss_reg

            # check early stop
            if self.early_stop:  # pragma: no cover
                if reg_best < float("inf"):
                    if reg_best >= self.early_stop_threshold * early_stop_reg_best:
                        early_stop_counter += 1
                    else:
                        early_stop_counter = 0
                early_stop_reg_best = min(reg_best, early_stop_reg_best)

                if cost_down_flag and cost_up_flag and early_stop_counter >= self.early_stop_patience:
                    logger.info("Early stop")
                    break

            # cost modification
            if avg_loss_acc >= self.attack_success_threshold:
                cost_set_counter += 1
                if cost_set_counter >= self.patience:
                    self.cost = self.init_cost
                    K.set_value(self.cost_tensor, self.cost)
                    cost_up_counter = 0
                    cost_down_counter = 0
                    cost_up_flag = False
                    cost_down_flag = False
            else:
                cost_set_counter = 0

            if avg_loss_acc >= self.attack_success_threshold:
                cost_up_counter += 1
                cost_down_counter = 0
            else:
                cost_up_counter = 0
                cost_down_counter += 1

            if cost_up_counter >= self.patience:
                cost_up_counter = 0
                self.cost *= self.cost_multiplier_up
                K.set_value(self.cost_tensor, self.cost)
                cost_up_flag = True
            elif cost_down_counter >= self.patience:
                cost_down_counter = 0
                self.cost /= self.cost_multiplier_down
                K.set_value(self.cost_tensor, self.cost)
                cost_down_flag = True

        if mask_best is None:
            mask_best = K.eval(self.mask_tensor)
            pattern_best = K.eval(self.pattern_tensor)

        if pattern_best is None:
            raise ValueError("Unexpected `None` detected.")

        return mask_best, pattern_best
baseKernels = []
baseKernels.append((1, 4, 6, 4, 1))
baseKernels.append((1, -2, 0, -2, 1))
baseKernels.append((-1, 0, 2, 0, -1))
baseKernels.append((1, -4, 6, -4, 1))
baseKernels = np.array(baseKernels)

#get directory of input images and create array of images and store images in the directory to the array
train_dir = "C:/Users/panka/OneDrive/Desktop/Aditya/image data 2018-19/Train_Resized"
#get labels pickle and convert to dataframe then sort by the filename to go along with the images
train_labels_file = "C:/Users/panka/OneDrive/Desktop/Aditya/image data 2018-19/Training_Input_Resized.pkl"

train_labels = pd.read_pickle(train_labels_file)

train_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_dataframe(
    dataframe=train_labels,
    directory=train_dir,
    target_size=(108, 192),
    x_col='Filename',
    y_col=[
        'Right Ankle x', 'Right Knee x', 'Right Hip x', 'Left Hip x',
        'Left Knee x', 'Left Ankle x', 'Pelvis x', 'Thorax x', 'Upper Neck x',
        'Head Top x', 'Right Wrist x', 'Right Elbow x', 'Right Shoulder x',
        'Left Shoulder x', 'Left Elbow x', 'Left Wrist x', 'Right Ankle y',
        'Right Knee y', 'Right Hip y', 'Left Hip y', 'Left Knee y',
        'Left Ankle y', 'Pelvis y', 'Thorax y', 'Upper Neck y', 'Head Top y',
        'Right Wrist y', 'Right Elbow y', 'Right Shoulder y',
        'Left Shoulder y', 'Left Elbow y', 'Left Wrist y'
    ],
Exemple #16
0
import tensorflow as tf
from keras.optimizers import RMSprop
from keras_preprocessing.image import ImageDataGenerator
import numpy as np
from google.colab import files
from keras_preprocessing import image

train_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(train_dir,
                                                    target_size=(300, 300),
                                                    batch_size=128,
                                                    class_mode='binary')
test_datagen = ImageDataGenerator(rescale=1. / 255)
validation_generator = test_datagen.flow_from_directory(validation_dir,
                                                        target_size=(300, 300),
                                                        batch_size=32,
                                                        class_mode='binary')

model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16, (3, 3), activation='relu',
                                                           input_shape=(300, 300, 3)),
                                    tf.keras.layers.MaxPooling2D(2, 2),
                                    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
                                    tf.keras.layers.MaxPooling2D(2, 2),
                                    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
                                    tf.keras.layers.MaxPooling2D(2, 2),
                                    tf.keras.layers.Flatten(),
                                    tf.keras.layers.Dense(512, activation='relu'),
                                    tf.keras.layers.Dense(1, activation='sigmoid')
                                    ])
Exemple #17
0
for i, img_path in enumerate(next_rock + next_paper + next_scissors):
    # print(img_path)
    img = mpimg.imread(img_path)
    plt.imshow(img)
    plt.axis('Off')
    plt.show()


# %%
import tensorflow as tf
import keras_preprocessing
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator

TRAINING_DIR = os.path.join(base_dir, "rps")
training_datagen = ImageDataGenerator(rescale=1. / 255,
                                      rotation_range=0.2,
                                      width_shift_range=0.2,
                                      height_shift_range=0.2,
                                      shear_range=0.2,
                                      zoom_range=0.2,
                                      horizontal_flip=True,
                                      fill_mode='nearest')

VALIDATION_DIR = os.path.join(base_dir, "rps-test-set")
validation_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = training_datagen.flow_from_directory(TRAINING_DIR,
                                                       target_size=(150, 150),
                                                       class_mode="categorical",
                                                       batch_size=126)
def train():
    ############### 1. Generating captchas

    images, labels = get_data_set(width=img_x,
                                  height=img_y,
                                  nr_of_chars=nr_of_chars,
                                  color=color,
                                  nr_of_captchas=numberOfCaptchas,
                                  number=numbers)

    ############### 2. Preprocessing the data
    #
    # Data needs to be reshaped into a 4D tensor - (sample_number, x_img_size, y_img_size, num_channels)
    # The number of channels = number of colors grescale = 1, color = 3
    images_train = images[:testSize]
    print('Before reshaping, 3D :', images_train.shape)
    images_train = images_train.reshape(images_train.shape[0], img_x, img_y,
                                        depth)
    images_train = images_train.astype(
        'float32') / 255  # Scaling color dimension to 0-255 to 0-1
    print('After reshaping, 3D :', images_train[0].shape)

    images_test = images[testSize:]
    images_test = images_test.reshape(images_test.shape[0], img_x, img_y,
                                      depth)
    images_test = images_test.astype('float32') / 255

    labels_train = labels[:testSize]
    labels_test = labels[testSize:]

    # The categories are characters [aa, ab, ac, ... ]
    categories = get_all_possible_label_categories(nr_of_chars, number=numbers)
    lb = LabelBinarizer().fit(categories)
    labels_encoded_train = lb.transform(labels_train)
    labels_encoded_test = lb.transform(labels_test)

    # Data generator for image augmentation
    datagen = ImageDataGenerator(
        rotation_range=10,  #15 graden rotatie van image
    )
    gen_train = datagen.flow(images_train,
                             labels_encoded_train,
                             batch_size=100)

    ############### 3. Building the neural network
    # Sequential model
    model = Sequential()
    # First conv: 32 filters of 3x3
    model.add(
        Conv2D(16, (5, 5),
               padding='same',
               input_shape=(img_x, img_y, 1),
               activation="relu"))
    model.add(Dropout(0.3))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Second conv
    model.add(layers.Conv2D(32, (5, 5), padding='same', activation='relu'))
    model.add(Dropout(0.3))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Third conv
    model.add(layers.Conv2D(64, (5, 5), padding='same', activation='relu'))
    model.add(Dropout(0.3))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Fully connected layer
    model.add(Flatten())
    model.add(Dense(512, activation="relu"))
    model.add(Dropout(0.5))
    model.add(Dense(len(categories), activation="softmax"))

    print(model.summary())
    model.compile(loss="categorical_crossentropy",
                  optimizer=optimizers.Adam(),
                  metrics=["accuracy"])

    ############### 4. Training the neural network
    history = model.fit_generator(
        gen_train,
        validation_data=(images_test, labels_encoded_test),
        steps_per_epoch=40,  #40 * 100 = 4000 == size trainingsdata
        epochs=80)

    ############### 5. Visualizing and saving the result
    # Retrieving the acc and loss
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']

    # Creating the output dir for this model
    if not numbers:
        model_dir = os.path.join(
            output_dir, 'model_chars' + str(nr_of_chars) + '_acc_' +
            str(max(val_acc)).replace('.', '_')[:4])
    else:
        model_dir = os.path.join(
            output_dir, 'model_nrs' + str(nr_of_chars) + '_acc_' +
            str(max(val_acc)).replace('.', '_')[:4])

    if os.path.exists(model_dir):
        model_dir = os.path.join(model_dir, '_' + str(time.time()))

    os.mkdir(model_dir)

    model.save(os.path.join(model_dir, 'model.h5'))
    model_json = model.to_json()
    copyfile('./captcha_learner_1.py',
             os.path.join(model_dir, 'captcha_learner_1.py'))
    with open(os.path.join(model_dir, 'model.json'), "w") as json_file:
        json_file.write(model_json)

    epochs = range(1, len(acc) + 1)
    plt.figure()
    plt.subplot(211)
    plt.plot(epochs, acc, 'bo', label='Training acc')
    plt.plot(epochs, val_acc, 'b', label='Validation acc')
    plt.title('Training and validation accuracy')
    plt.legend()

    plt.subplot(212)

    plt.plot(epochs, loss, 'bo', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Validation loss')
    plt.title('Training and validation loss')
    plt.legend()
    plt.savefig(os.path.join(model_dir, 'metrics.png'))
    plt.show()
from tensorflow.keras import layers
from keras_preprocessing.image import ImageDataGenerator

# This is the code that sets up the dataframes to give us our data generators

train_dir = '/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'
test_dir = '/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'
df_train = pd.read_csv(
    '/kaggle/input/siim-isic-melanoma-classification/train.csv')
df_test = pd.read_csv(
    '/kaggle/input/siim-isic-melanoma-classification/test.csv')
df_train['image_num'] = df_train.apply(
    lambda row: train_dir + row['image_name'] + '.jpg', axis=1)
df_test['image_num'] = df_test.apply(
    lambda row: test_dir + row['image_name'] + '.jpg', axis=1)
datagen = ImageDataGenerator(rescale=1. / 255., validation_split=0.25)

train_generator = datagen.flow_from_dataframe(dataframe=df_train,
                                              x_col="image_num",
                                              y_col="target",
                                              subset="training",
                                              batch_size=32,
                                              seed=42,
                                              shuffle=True,
                                              class_mode="raw",
                                              target_size=(224, 224))

valid_generator = datagen.flow_from_dataframe(dataframe=df_train,
                                              x_col="image_num",
                                              y_col="target",
                                              subset="validation",
    '/home/shubrah/bird/REWB_1/338f25d0-afb1-42f9-9d66-98aa54af9348.wav',
    '/home/shubrah/bird/YEBB_1/23cc7059-ad02-4b7d-905a-edb8503d4f69.wav',
    '/home/shubrah/bird/INSB_1/1e526283-de1b-47f4-a6c8-1fad0fa5d714.wav',
    '/home/shubrah/bird/INSB_1/c9a731b0-699b-49c8-8b87-d48ad45d8288.wav',
    '/home/shubrah/bird/LABC_1/80404893-0cbb-42b3-b9c1-f95e069fa90a.wav',
    '/home/shubrah/bird/LABC_2/d244afaf-7fa4-4c34-8ee7-d69682d9c2f8.wav',
    '/home/shubrah/bird/GRTD_s/22ba0fdb-42d6-471f-ad57-603bfa139b1e.wav'
]

for i in range(len(extract)):
    create_spectrogram(extract[i], extract[i].split('/')[5].split('.')[0])

traindf = create_df(extract, 'train')

from keras_preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1. / 255., validation_split=0.25)

train_generator = datagen.flow_from_dataframe(
    dataframe=traindf,
    directory="/home/shubrah/ml/pad/train/",
    x_col="filename",
    y_col="class",
    subset="training",
    batch_size=32,
    seed=42,
    shuffle=True,
    class_mode="categorical",
    target_size=(64, 64))

valid_generator = datagen.flow_from_dataframe(
    dataframe=traindf,
    'teenager', 'youth', 'middle_age', 'senior', 'white', 'black', 'asian',
    'oval_face', 'round_face', 'heart_face', 'smiling', 'mouth_open',
    'frowning', 'wearing_glasses', 'wearing_sunglasses', 'wearing_lipstick',
    'tongue_out', 'duck_face', 'black_hair', 'blond_hair', 'brown_hair',
    'red_hair', 'curly_hair', 'straight_hair', 'braid_hair',
    'showing_cellphone', 'using_earphone', 'using_mirror', 'braces',
    'wearing_hat', 'harsh_lighting', 'dim_lighting'
]
#Lendo o arquivo txt, separando ele por espacos e colocando num dicionario com o nome de cada atributo
data = pd.read_csv(path + 'selfie_dataset.txt',
                   header=None,
                   sep=' ',
                   names=names)

train_datagen = ImageDataGenerator(rescale=1. / 255,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)


#aplicar a extensao da imagem
def append_ext(fn):
    return fn + ".jpg"


#substituir o score pelas classes (boa, neutra, ruim)
def apply_class(fn):
    if fn > 4.7:
        return 'boa'
    elif fn > 3.5:
#loading the trained models
model_fruit = load_model('models/fruits-classifier-model.h5', compile=False)
model_orange = load_model("models/oranges-classifier-model.h5", compile=False)

#setting the size to which the images will be resized and the batch size
img_rows, img_cols = 32, 32
batch_size = 20

#getting the labels of the fruit classifier model
train_dataset_fruit = 'datasets/fruits/fruits/Train'
test_dataset_fruit = 'datasets/fruits/fruits/Test'

train_data_fruit = ImageDataGenerator(rescale=1. / 255,
                                      rotation_range=30,
                                      width_shift_range=0.3,
                                      height_shift_range=0.3,
                                      horizontal_flip=True,
                                      fill_mode='nearest')

train_generator_fruit = train_data_fruit.flow_from_directory(
    train_dataset_fruit,
    target_size=(img_rows, img_cols),
    batch_size=batch_size,
    class_mode='categorical',
    shuffle=True)

test_data_fruit = ImageDataGenerator(rescale=1. / 255)

test_generator = test_data_fruit.flow_from_directory(test_dataset_fruit,
                                                     target_size=(img_rows,
                                                                  img_cols),
Exemple #23
0
    model.add(Flatten())
    model.add(
        Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.5)))
    model.add(BatchNormalization())
    model.add(Dropout(0.25))
    model.add(Dense(3, activation='softmax'))

    # Show a summary of the model. Check the number of trainable parameters
    model.summary()

    train_dir = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data\\data\\Train\\'
    val_dir = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data\\data\\Validation\\'
    test_dir = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data\\data\\Test\\'

    train_datagen = ImageDataGenerator(horizontal_flip=True,
                                       zoom_range=0.1,
                                       preprocessing_function=preprocess_input)

    validation_datagen = ImageDataGenerator(
        preprocessing_function=preprocess_input)

    test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

    # Change the batchsize according to your system RAM
    train_batchsize = 8
    val_batchsize = 8

    train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(img_shape[0], img_shape[1]),
        batch_size=train_batchsize,
Exemple #24
0
    noise = np.random.normal(0, deviation, img.shape)
    img += noise
    np.clip(img, 0., 255.)
    return img


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    train_photos = np.array(load_files(train_data_path))
    sample = GaussianNoise(20, dtype=tf.float64)

    # Image transformer
    datagen = ImageDataGenerator(
        shear_range=0.2,
        zoom_range=[0.75, 1],
        rotation_range=5,
        horizontal_flip=True,
        dtype='uint8',
        preprocessing_function=lambda x: random_hue(x, seed=9, max_delta=0.05))
    # datagen = ImageDataGenerator(
    #     shear_range=0.0,
    #     zoom_range=0.0,
    #     rotation_range=0,
    #     horizontal_flip=False,
    #     dtype='uint8'
    # )

    #train_photos_lab = np.array([cv2.cvtColor(image, cv2.COLOR_RGB2Lab) for image in  train_photos])
    # del train_photos
    #
    test_photos_color = np.array(load_files(test_data_path))
Exemple #25
0
from PIL import Image
import os, glob, numpy as np
from keras_preprocessing.image import ImageDataGenerator, load_img
import matplotlib.pyplot as plt

img_dir = 'C:/cpi_image_test2'
categories = ['BW_image', 'no_BW_image']
np_classes = len(categories)

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

data_aug_gen = ImageDataGenerator(rescale=1. / 255,
                                  rotation_range=0,
                                  width_shift_range=0.1,
                                  height_shift_range=0.1,
                                  shear_range=0.5,
                                  zoom_range=[0.8, 2.0],
                                  horizontal_flip=True,
                                  vertical_flip=False,
                                  fill_mode='nearest')

img_dir = 'C:/cpi_image_test2'
img_dir_detail = img_dir + "/" + "BW_image" + "/"
files = glob.glob(img_dir_detail + '*.png')
save_to_dir = 'C:/cpi_image_test2/BW_image'

## DATA Argumentation
for i, f in enumerate(files):
    png_name = ''.join(f.split()).split('\\')[1][:-4]
    save_to_dir1 = save_to_dir + '/' + png_name
    if not os.path.exists(save_to_dir1):
target_size = (160, 270)

img = image.load_img(imagePath)
plt.imshow(img)
img = image.load_img(imagePath, target_size=target_size, interpolation="lanczos")
plt.imshow(img)

train_datagen = ImageDataGenerator(
                                  rescale = 1./255,
                                  # zoom_range=0.2,
                                  # rotation_range=40,
                                  # horizontal_flip=True,
                                  # width_shift_range=0.2,
                                  # height_shift_range=0.2,
                                  # shear_range=0.2,                                  
                                  fill_mode='nearest',
                                   
                                  # featurewise_center=False,  # set input mean to 0 over the dataset
                                  # samplewise_center=False,  # set each sample mean to 0
                                  # featurewise_std_normalization=False,  # divide inputs by std of the dataset
                                  # samplewise_std_normalization=False,  # divide each input by its std
                                  # zca_whitening=False,  # apply ZCA whitening
                                  # vertical_flip=False
                                  )

validation_datagen = ImageDataGenerator(
                                        rescale = 1./255,
                                        )

train_generator = train_datagen.flow_from_directory(
	training_path,
Exemple #27
0
class TimeSeriesGenerator(Sequence):
    def __init__(self,
                 zip_file: zipfile.ZipFile,
                 batch_size: int,
                 frames: list,
                 target_classes: list,
                 target_classes_rules: dict,
                 img_height,
                 img_width,
                 aug=None,
                 series_len=1,
                 series_interval=1):
        self.zip_file = zip_file
        self.aug = aug
        self.class_num = len(target_classes)
        self.is_softmax_activation = self.class_num > 1

        self.batch_size = batch_size
        self.series_len = series_len
        self.img_width = img_width
        self.img_height = img_height

        self._class_mapping = self._get_class_mapping(target_classes,
                                                      target_classes_rules)
        self._samples = self._get_samples(frames, series_len, series_interval)
        self._image_processor = ImageDataGenerator(**(aug or {}))

    def _get_samples(self, frames, series_len, series_interval):
        samples = []

        # parse frame names
        pattern = r'(?P<video_name>video.+)_frame_(?P<frame>\d+)'
        labelled_frames = (re.match(pattern, f) for f in frames)
        labelled_frames = [{
            'video_name': m.group('video_name'),
            'frame': int(m.group('frame')),
            'name': m.string
        } for m in labelled_frames]

        # read video fps info from auxilary json file
        video_fps = self.zip_file.read('video_fps.json').decode('utf-8')
        video_fps = json.loads(video_fps)

        all_frames = set(f for f in self.zip_file.namelist()
                         if f.startswith('images/'))
        all_masks = set(f for f in self.zip_file.namelist()
                        if f.startswith('segments/'))
        # create samples from labelled frames
        for frame in labelled_frames:
            series_step = int(video_fps[frame['video_name']] * series_interval)
            series_end = frame['frame']
            series_start = max(series_end - series_step * (series_len - 1), 0)
            frames_idx = [
                *range(series_start, series_end, series_step), series_end
            ]
            if len(frames_idx) == series_len:
                series = [
                    '{}_frame_{}'.format(frame['video_name'], idx)
                    for idx in frames_idx
                ]

                # make sure all images and masks exist before process
                assert all('images/{}.jpg'.format(f) in all_frames for f in series),\
                    'images sequence not found: video {video_name}, frame {frame}'.format(**frame)
                assert 'segments/{}.png'.format(series[-1]) in all_masks,\
                    'segments not found: video {video_name}, frame {frame}'.format(**frame)

                samples.append(series)

        return samples

    def _get_class_mapping(self, target_classes, target_classes_rules):
        mapping = {}

        #read labels
        labels = self.zip_file.read('labels.txt').decode('utf-8').split('\r\n')
        labels = {
            int(l.split(':')[0]): l.split(':')[1].lstrip(' ')
            for l in labels
        }

        for target_idx, target_class in enumerate(target_classes):
            if target_class in target_classes_rules:
                for rule_list_item in target_classes_rules[target_class]:
                    label_idx = next((idx for idx, label in labels.items()
                                      if label == rule_list_item), None)
                    if label_idx is not None:
                        mapping[label_idx] = target_idx
                    else:
                        raise Exception(
                            "Can't find mapping target class '{}' with rule '{}' in labels: '{}'"
                            .format(target_class, rule_list_item,
                                    dict(labels)))
            else:
                # skip exception on background class
                if target_idx == 0 and labels[0] == 'background':
                    continue
                raise Exception(
                    "Can't find mapping rule for the class '{}' with rules '{}' and labels: '{}'"
                    .format(target_class, target_classes_rules, dict(labels)))

        for i in range(self.class_num):
            if i not in mapping.values():
                # minus in key mean that it is skipped, but could be used as background
                # extra -1 is used for the i==0 - there are no -0 with type int
                mapping[-i - 1] = i

        return mapping

    def _unpack_to_classes_with_mapping(self, mask):
        new_y = np.zeros(mask.shape[:2] + (self.class_num, ), dtype=mask.dtype)

        filled_mask = np.zeros(mask.shape[:2], dtype=np.bool_)

        items_to_map = sorted(self._class_mapping.items(),
                              key=lambda x: x[0],
                              reverse=True)
        for source_idx, target_idx in items_to_map:
            # filling class 0 with all not filled pixels when it has softmax activation
            if self.is_softmax_activation and target_idx == 0:
                new_y[:, :, target_idx][~filled_mask] = 1
                continue

            # updates only pixes with 0 values - it will allow to combine many masks into 1 without overwritten pixels
            update_mask = (new_y[:, :, target_idx] == 0) & (mask == source_idx)

            # disable pixel filling when it already filled by previous classes
            if self.is_softmax_activation:
                update_mask[update_mask & filled_mask] = False
                filled_mask = filled_mask | update_mask

            new_y[:, :, target_idx][update_mask] = 1

        return new_y

    def __len__(self):
        return int(np.ceil(len(self._samples) / self.batch_size))

    def __getitem__(self, idx):
        batch_start = idx * self.batch_size
        batch_end = min(batch_start + self.batch_size, len(self._samples))

        x_batch = np.zeros((batch_end - batch_start, self.series_len,
                            self.img_height, self.img_width, 3),
                           dtype=np.float32)
        y_batch = np.zeros((batch_end - batch_start, self.img_height,
                            self.img_width, self.class_num),
                           dtype=np.uint8)

        for batch_idx, sample_idx in enumerate(range(batch_start, batch_end)):
            # apply same tranformations to all frames in a sample
            transform_params = self._image_processor.get_random_transform(
                (self.img_height, self.img_width))

            for series_idx, frame_name in enumerate(self._samples[sample_idx]):
                # load image
                img_byte_string = self.zip_file.read(
                    'images/{}.jpg'.format(frame_name))
                img = np.frombuffer(img_byte_string, dtype=np.uint8)
                img = cv2.imdecode(img, cv2.IMREAD_COLOR)
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

                # resize to output size
                img = cv2.resize(img, (self.img_width, self.img_height),
                                 interpolation=cv2.INTER_LINEAR)

                # apply augmentation
                img = self._image_processor.apply_transform(
                    img, transform_params)

                x_batch[batch_idx, series_idx, :, :, :] = img

            # load mask
            mask_byte_string = self.zip_file.read('segments/{}.png'.format(
                self._samples[sample_idx][-1]))
            mask = np.frombuffer(mask_byte_string, dtype=np.uint8)
            mask = cv2.imdecode(mask, cv2.IMREAD_GRAYSCALE)

            # resize to output size
            mask = cv2.resize(mask, (self.img_width, self.img_height),
                              interpolation=cv2.INTER_NEAREST)

            # transform graysacale mask to class_num channel mask
            mask = self._unpack_to_classes_with_mapping(mask)

            # disable some transformations for mask and apply augmentation
            transform_params['channel_shift_intensity'] = None
            transform_params['brightness'] = None
            mask = self._image_processor.apply_transform(
                mask, transform_params)

            y_batch[batch_idx, :, :, :] = mask

        return x_batch, y_batch
Exemple #28
0
    train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
    train_labels.label = train_labels.label.astype('str')

    Y = train_labels[['label']]
    n = len(train_labels)
    skf = StratifiedKFold(n_splits=5)

    valid_accuracy = []
    valid_loss = []
    fold_var = 1

    train_idg = ImageDataGenerator(
        preprocessing_function=None,
        rotation_range=45,
        zoom_range=0.2,
        horizontal_flip=True,
        vertical_flip=True,
        fill_mode='nearest',
        shear_range=0.1,
        height_shift_range=0.1,
        width_shift_range=0.1)

    validation_idg = ImageDataGenerator(validation_split=0.2)

    for train_index, val_index in skf.split(np.zeros(n), Y):
        training_data = train_labels.iloc[train_index]
        validation_data = train_labels.iloc[val_index]

        train_generator = train_idg.flow_from_dataframe(training_data,
                                                        directory=os.path.join(WORK_DIR, "train_images"),
                                                        subset="training",
                                                        x_col="image_id",
    # print(img_path)
    img = mpimg.imread(img_path)
    plt.imshow(img)
    plt.axis('Off')
    plt.show()

import tensorflow as tf
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator

TRAINING_DIR = r'C:\Users\kirin\tmp\rps'
training_datagen = ImageDataGenerator(
    rescale = 1./255,
    rotation_range = 40,
    width_shift_range = 0.2,
    height_shift_range = 0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

VALIDATION_DIR = r'C:\Users\kirin\tmp\rps-test-set'
validation_datagen = ImageDataGenerator(rescale=1./255)

train_generator = training_datagen.flow_from_directory(
    TRAINING_DIR,
    target_size=(150,150),
    class_mode='categorical'
)

validation_generator = validation_datagen.flow_from_directory(
    plt.show()
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()


if __name__ == '__main__':
    x_train, y_train, x_test, y_test = load_and_preprocess_data()
    model = construct_model()

    train_datagen = ImageDataGenerator(rotation_range=15,
                                       width_shift_range=0.1,
                                       height_shift_range=0.1,
                                       horizontal_flip=True)

    batch_size = 64
    train_generator = train_datagen.flow(x=x_train,
                                         y=y_train,
                                         batch_size=batch_size,
                                         shuffle=True)

    checkpointer = ModelCheckpoint(
        filepath="models/{epoch:02d}-{val_acc:.4f}-{val_loss:.4f}.hd5",
        save_weights_only=False,
        verbose=1,
        save_best_only=False)

    reduce_lr = ReduceLROnPlateau(monitor='val_acc',