def get_model(train=True): if Path('model.h5').is_file(): return load_model('model.h5') datagen = ImageDataGenerator( rotation_range=10, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.1, zoom_range=0.2, horizontal_flip=False, preprocessing_function=gen_preprocess, fill_mode='nearest') data_generator = datagen.flow_from_directory( directory='train_data/', target_size=IMG_SIZE, class_mode='categorical') print(data_generator.classes) validgen = ImageDataGenerator(preprocessing_function=gen_preprocess) valid_generator = validgen.flow_from_directory( directory='valid_data/', target_size=IMG_SIZE, class_mode='categorical', shuffle=False ) test_generator = validgen.flow_from_directory( directory='test_data/', target_size=IMG_SIZE, class_mode='categorical', shuffle=False ) model = SqueezeNet() print(model.summary()) x = Convolution2D(4, (1, 1), padding='same', name='conv11')(model.layers[-5].output) x = Activation('relu', name='relu_conv11')(x) x = GlobalAveragePooling2D()(x) x = Activation('softmax')(x) # x= Dense(4, activation='softmax')(x) # x = Dense(4, activation='softmax')(model.layers[-2].output) model = Model(model.inputs, x) print(model.summary()) # Following is the original model I was training # model = Sequential() # # model.add(Convolution2D(16, 3, 3, # border_mode='same', # input_shape=IMG_SHAPE)) # model.add(MaxPooling2D(pool_size=(3, 3))) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # model.add(Convolution2D(32, 3, 3, # border_mode='same')) # model.add(MaxPooling2D(pool_size=(3, 3))) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # model.add(Convolution2D(48, 3, 3, # border_mode='same')) # model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # # model.add(Convolution2D(64, 3, 3, # border_mode='same')) # model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # # model.add(Convolution2D(64, 3, 3, # border_mode='same')) # model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # # 1st Layer - Add a flatten layer # model.add(Flatten()) # # model.add(Dense(1164)) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # model.add(Dense(128)) # model.add(Activation('tanh')) # model.add(Dropout(0.2)) # # # 2nd Layer - Add a fully connected layer # model.add(Dense(50)) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # model.add(Dense(10)) # model.add(Activation('relu')) # model.add(Dropout(0.2)) # # # 4th Layer - Add a fully connected layer # model.add(Dense(4)) # # 5th Layer - Add a ReLU activation layer # model.add(Activation('softmax')) # TODO: Build a Multi-layer feedforward neural network with Keras here. # TODO: Compile and train the model filepath = "weights-improvement-{epoch:02d}-{loss:.2f}.hdf5" callbacks = [ EarlyStopping(monitor='loss', min_delta=0.01, patience=2, verbose=1), LambdaCallback(on_epoch_end=lambda batch,logs: evaluate_model(model, test_generator)), ModelCheckpoint(filepath=filepath, monitor='loss', save_best_only=True, verbose=1), ] model.compile(keras.optimizers.Adam(lr=0.0001), 'categorical_crossentropy', ['accuracy']) model.fit_generator(data_generator, steps_per_epoch=400, epochs=30, verbose=1, callbacks=callbacks) evaluate_model(model, test_generator) model.save('model.h5', True) return model
train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') train_history = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples / batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size) model.save_weights('navigation_cnn_squeezenet_EE_2F_4class_v2.h5') #view gradient descent import matplotlib.pyplot as plt def show_train_history(train_history, train, validation): plt.plot(train_history.history[train]) plt.plot(train_history.history[validation]) plt.title('Train History') plt.ylabel(train) plt.xlabel('Epoch')
target_size=(227, 227), # all images will be resized to 150x150 batch_size=batch_size, class_mode='binary' ) # since we use binary_crossentropy loss, we need binary labels # this is a similar generator, for validation data validation_generator = test_datagen.flow_from_directory(path + 'valid', target_size=(227, 227), batch_size=batch_size, class_mode='binary') steps = train_generator.samples // batch_size if steps == 0: steps = 1 val_steps = validation_generator.samples // batch_size if val_steps == 0: val_steps = 1 model.fit_generator(train_generator, steps_per_epoch=steps, epochs=1, validation_data=validation_generator, validation_steps=val_steps) model.save_weights( 'squeezecatsdogs.h5' ) # always save your weights after training or during training