# np.load x_train = np.load('../Project01_data/9.npy/cv_color_x_train_cocacola.npy') x_test = np.load('../Project01_data/9.npy/cv_color_x_test_cocacola.npy') x_valid = np.load('../Project01_data/9.npy/cv_color_x_valid_cocacola.npy') y_train = np.load('../Project01_data/9.npy/cv_color_y_train_cocacola.npy') y_test = np.load('../Project01_data/9.npy/cv_color_y_test_cocacola.npy') y_valid = np.load('../Project01_data/9.npy/cv_color_y_valid_cocacola.npy') print("x : ", x_train.shape, x_test.shape, x_valid.shape) print("y : ", y_train.shape, y_test.shape, y_valid.shape) # x : (341, 64, 64, 3) (43, 64, 64, 3) (38, 64, 64, 3) # y : (341,) (43,) (38,) batch = 16 train_generator = train_datagen.flow(x_train, y_train, batch_size=batch) test_generator = etc_datagen.flow(x_test, y_test, batch_size=batch) valid_generator = etc_datagen.flow(x_valid, y_valid) #2 Modeling # def modeling(drop1=0.2, node1=128, node2=64, activation1='relu', activation2='relu') : # def modeling(optimizer='adam', drop2=0.3, drop3=0.4, kernel1=2, kernel2=2, kernel3=2) : # def modeling(pool1=2, pool2=2, pool3=2) : model = Sequential() model.add(Conv2D(32, kernel1, padding='same', activation=activation1, input_shape=(x_train.shape[1], x_train.shape[2], x_train.shape[3]))) model.add(BatchNormalization()) model.add(Conv2D(32, kernel1, padding='same', activation=activation1)) model.add(BatchNormalization()) model.add(MaxPool2D(pool1)) model.add(Dropout(drop1))
# loop over all layers in the base model and freeze them so they will # *not* be updated during the first training process for layer in baseModel.layers: layer.trainable = False # compile our model print("[INFO] compiling model...") opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network print("[INFO] training head...") H = model.fit( aug.flow(trainX, trainY, batch_size=BS), steps_per_epoch=len(trainX) // BS, validation_data=(testX, testY), validation_steps=len(testX) // BS, epochs=EPOCHS) # make predictions on the testing set print("[INFO] evaluating network...") predIdxs = model.predict(testX, batch_size=BS) # for each image in the testing set we need to find the index of the # label with corresponding largest predicted probability predIdxs = np.argmax(predIdxs, axis=1) # show a nicely formatted classification report print(classification_report(testY.argmax(axis=1), predIdxs,
# the actual model we will train) model = Model(inputs=baseModel.input, outputs=headModel) # loop over all layers in the base model and freeze them so they will # *not* be updated during the first training process for layer in baseModel.layers: layer.trainable = False # compile our model print("[INFO] compiling model...") opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network print("[INFO] training head...") H = model.fit_generator(trainAug.flow(trainX, trainY, batch_size=BS), steps_per_epoch=len(trainX) // BS, validation_data=(testX, testY), validation_steps=len(testX) // BS, epochs=EPOCHS) # make predictions on the testing set print("[INFO] evaluating network...") predIdxs = model.predict(testX, batch_size=BS) # for each image in the testing set we need to find the index of the # label with corresponding largest predicted probability predIdxs = np.argmax(predIdxs, axis=1) # show a nicely formatted classification report print(
def train(self, epochs=100, batch_size=32, split_size=0.10, checkpoint_path=None, early_stopping=True, verbose=1): ''' Train the model on the given data. :param epochs: Number of epochs. :param batch_size: Number of samples per batch. :param split_size: Test/Train proportion. :param checkpoint_path: Path where to save the checkpoints. :param early_stopping: Boolean, should we use early stopping if no progress is made. :param verbose: 0:=no output, 1:=errors only, 2:= everything. :return: `hist`, `model`, The metrics history and the model. ''' # One-hot encoding: Basically "unique-fy-ish" each class. # https://hackernoon.com/what-is-one-hot-encoding-why-and-when-do-you-have-to-use-it-e3c6186d008f class MyLabelBinarizer(LabelBinarizer): def transform(self, y): Y = super().transform(y) if self.y_type_ == 'binary': return np.hstack((Y, 1 - Y)) else: return Y def inverse_transform(self, Y, threshold=None): if self.y_type_ == 'binary': return super().inverse_transform(Y[:, 0], threshold) else: return super().inverse_transform(Y, threshold) chosen_binarizer = MyLabelBinarizer() train_labels_fit = chosen_binarizer.fit_transform(self.ImageData.labels) self.classes = chosen_binarizer.classes_ if verbose == 2: print("Classes: ", self.classes) # Test and train set, yay. X_train, X_test, y_train, y_test = train_test_split(self.ImageData.images, train_labels_fit, test_size=split_size) if verbose == 2: print("Shapes:", X_train.shape, X_test.shape, y_train.shape, y_test.shape) # Dataset augmentation # This may not be needed following some magazines, as we do not often have rotated texts... # ... Or do we? Anyway it applies for the pictures so there's that. imgdatagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=90) train_data = imgdatagen.flow(x=X_train, y=y_train, batch_size=batch_size) imgdatagen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=90) test_data = imgdatagen.flow(x=X_test, y=y_test, batch_size=batch_size) callbacks = [] # We want checkpoints because losing training suckz lolz. https://keras.io/callbacks/#modelcheckpoint if checkpoint_path is not None: callbacks.append(ModelCheckpoint(checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)) # If we are not doing any progress, stops the whole thing. https://keras.io/callbacks/#earlystopping if early_stopping: callbacks.append(EarlyStopping(monitor='val_loss', min_delta=0, patience=epochs/10, verbose=1, mode='auto')) # FINALLY train the model. https://keras.io/models/sequential/#fit_generator steps = ceil(len(train_data) / batch_size) self.hist = self.model.fit_generator(generator=train_data, steps_per_epoch=steps, epochs=epochs, verbose=1, validation_data=test_data, validation_steps=steps, callbacks=callbacks) return self.hist, self.model
samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=rotation, shear_range=0.2, zoom_range=(0.8, 1.2), fill_mode='reflect', width_shift_range=8. / 32, height_shift_range=8. / 32, horizontal_flip=True, vertical_flip=False, data_format=data_format) datagen.fit(data.train_data, augment=True) data_flow = datagen.flow(data.train_data, data.train_labels, batch_size=128, shuffle=True) if is_distillation: print("train init model") if not os.path.exists( os.path.join(save_model_dir, save_model_name + '_init')): train_wrn(data_flow, os.path.join(save_model_dir, save_model_name + '_init'), num_epochs=1, debug=False, gpus=selected_gpus, data_format=data_format, rand_spike=rspike, dropout=dropout) print("train teacher model")
# initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=config.MIN_LR, momentum=0.9) model = MiniGoogLeNet.build(width=32, height=32, depth=1, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # check to see if we are attempting to find an optimal learning rate # before training for the full number of epochs if args["lr_find"] > 0: # initialize the learning rate finder and then train with learning # rates ranging from 1e-10 to 1e+1 print("[INFO] finding learning rate...") lrf = LearningRateFinder(model) lrf.find(aug.flow(trainX, trainY, batch_size=config.BATCH_SIZE), 1e-10, 1e+1, stepsPerEpoch=np.ceil((len(trainX) / float(config.BATCH_SIZE))), batchSize=config.BATCH_SIZE) # plot the loss for the various learning rates and save the # resulting plot to disk lrf.plot_loss() plt.savefig(config.LRFIND_PLOT_PATH) # gracefully exit the script so we can adjust our learning rates # in the config and then train the network for our full set of # epochs print("[INFO] learning rate finder complete") print("[INFO] examine plot and adjust learning rates before training")
''' Set learning rate and compile the model''' opt = optimizers.SGD(lr=0.01, momentum=0.9) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=(['accuracy'])) #%% ''' Train the model ''' # Data augmentation idg = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True, rotation_range=20, zoom_range=0.1, fill_mode='nearest') itd = idg.flow(trainX, trainY, batch_size=64) n_epochs = 100 history = model.fit_generator(itd, steps_per_epoch=int(trainX.shape[0] / 64), epochs=n_epochs, validation_data=(validX, validY), verbose=1) #%% ''' Plot model's performance statistics ''' plt.figure(figsize=(10, 10)) # Plot model's accuracy information based on epochs plt.subplot(211) plt.title('Accuracy evolution') plt.plot(history.history['accuracy'], label='training accuracy')
#opt=SGD(lr=INIT_LR, decay=INIT_LR / (NUM_EPOCHS * 0.5),momentum=0.9, nesterov=True) model = TrafficSignNet.build(width=64, height=64, depth=3,classes=numLabels) model.compile(loss="categorical_crossentropy", optimizer=opt,metrics=["accuracy"]) #trainX=np.expand_dims(trainX,axis=3) #trainY=np.expand_dims(trainY,axis=2) #testX=np.expand_dims(testX,axis=3) #trainX=np.expand_dims(trainX,axis=3) model_checkpoint = ModelCheckpoint('Daksh1.h5', monitor='loss',verbose=1, save_best_only=True) history=model.fit(aug.flow(trainX,trainY,batch_size=BS),epochs=NUM_EPOCHS,callbacks=[model_checkpoint],verbose=1) #callbacks=[model_checkpoint] # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=BS) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames)) # save the network to disk #model.save(r"C:\Users\Indrajithu\Downloads\Grievance_model\Grievance_model\model.json") from keras.models import model_from_json from keras.models import save_model,load_model model_json = model.to_json() with open("model.json", "w") as json_file:
def main(): parser = argparse.ArgumentParser(description='Model trainer') parser.add_argument('--input_dir', help='Processed data directory') parser.add_argument('--output_dir', help='Output model directory') parser.add_argument('--epochs', help='Number of training epochs') parser.add_argument('--model_name', help='Output model name') parser.add_argument('--model_version', help='Output model version') args = parser.parse_args() print('output_dir, model_name') print(args.output_dir, args.model_name) # print('device_lib.list_local_devices', device_lib.list_local_devices()) #sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) #keras.backend.set_session(sess) print('K.tensorflow_backend._get_available_gpus', K.tensorflow_backend._get_available_gpus()) # Copy TRTIS resource (containing config.pbtxt, labels.txt, ...) from container to mounted volume model_dir = os.path.join(args.output_dir, args.model_name) if os.path.isdir(model_dir): shutil.rmtree(model_dir) shutil.copytree(CONT_TRTIS_RESOURCE_DIR, model_dir) os.mkdir(os.path.join(model_dir, args.model_version)) # Training parameters batch_size = 128 # orig paper trained all networks with batch_size=128 epochs = int(args.epochs) data_augmentation = True num_classes = 10 # Subtracting pixel mean improves accuracy subtract_pixel_mean = True # Model parameter # ---------------------------------------------------------------------------- # | | 200-epoch | Orig Paper| 200-epoch | Orig Paper| sec/epoch # Model | n | ResNet v1 | ResNet v1 | ResNet v2 | ResNet v2 | GTX1080Ti # |v1(v2)| %Accuracy | %Accuracy | %Accuracy | %Accuracy | v1 (v2) # ---------------------------------------------------------------------------- # ResNet20 | 3 (2)| 92.16 | 91.25 | ----- | ----- | 35 (---) # ResNet32 | 5(NA)| 92.46 | 92.49 | NA | NA | 50 ( NA) # ResNet44 | 7(NA)| 92.50 | 92.83 | NA | NA | 70 ( NA) # ResNet56 | 9 (6)| 92.71 | 93.03 | 93.01 | NA | 90 (100) # ResNet110 |18(12)| 92.65 | 93.39+-.16| 93.15 | 93.63 | 165(180) # ResNet164 |27(18)| ----- | 94.07 | ----- | 94.54 | ---(---) # ResNet1001| (111)| ----- | 92.39 | ----- | 95.08+-.14| ---(---) # --------------------------------------------------------------------------- n = 3 # Model version # Orig paper: version = 1 (ResNet v1), Improved ResNet: version = 2 (ResNet v2) version = 2 # Computed depth from supplied model parameter n if version == 1: depth = n * 6 + 2 elif version == 2: depth = n * 9 + 2 # Model name, depth and version model_type = 'ResNet%dv%d' % (depth, version) # Load the CIFAR10 data. def load_preprocessed_data(input_dir): x_train = np.load(os.path.join(input_dir, "x_train.npy")) y_train = np.load(os.path.join(input_dir, "y_train.npy")) x_test = np.load(os.path.join(input_dir, "x_test.npy")) y_test = np.load(os.path.join(input_dir, "y_test.npy")) return x_train, y_train, x_test, y_test preprocessed_data = load_preprocessed_data(args.input_dir) x_train, y_train, x_test, y_test = preprocessed_data # Input image dimensions. input_shape = x_train.shape[1:] # Normalize data. x_train = x_train.astype('float32') / 255 x_test = x_test.astype('float32') / 255 # If subtract pixel mean is enabled if subtract_pixel_mean: x_train_mean = np.mean(x_train, axis=0) x_train -= x_train_mean x_test -= x_train_mean print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') print('y_train shape:', y_train.shape) # Convert class vectors to binary class matrices. y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) def lr_schedule(epoch): """Learning Rate Schedule Learning rate is scheduled to be reduced after 80, 120, 160, 180 epochs. Called automatically every epoch as part of callbacks during training. # Arguments epoch (int): The number of epochs # Returns lr (float32): learning rate """ lr = 1e-3 if epoch > 180: lr *= 0.5e-3 elif epoch > 160: lr *= 1e-3 elif epoch > 120: lr *= 1e-2 elif epoch > 80: lr *= 1e-1 print('Learning rate: ', lr) return lr def resnet_layer(inputs, num_filters=16, kernel_size=3, strides=1, activation='relu', batch_normalization=True, conv_first=True): """2D Convolution-Batch Normalization-Activation stack builder # Arguments inputs (tensor): input tensor from input image or previous layer num_filters (int): Conv2D number of filters kernel_size (int): Conv2D square kernel dimensions strides (int): Conv2D square stride dimensions activation (string): activation name batch_normalization (bool): whether to include batch normalization conv_first (bool): conv-bn-activation (True) or bn-activation-conv (False) # Returns x (tensor): tensor as input to the next layer """ conv = Conv2D(num_filters, kernel_size=kernel_size, strides=strides, padding='same', kernel_initializer='he_normal', kernel_regularizer=l2(1e-4)) x = inputs if conv_first: x = conv(x) if batch_normalization: x = BatchNormalization()(x) if activation is not None: x = Activation(activation)(x) else: if batch_normalization: x = BatchNormalization()(x) if activation is not None: x = Activation(activation)(x) x = conv(x) return x def resnet_v1(input_shape, depth, num_classes=10): """ResNet Version 1 Model builder [a] Stacks of 2 x (3 x 3) Conv2D-BN-ReLU Last ReLU is after the shortcut connection. At the beginning of each stage, the feature map size is halved (downsampled) by a convolutional layer with strides=2, while the number of filters is doubled. Within each stage, the layers have the same number filters and the same number of filters. Features maps sizes: stage 0: 32x32, 16 stage 1: 16x16, 32 stage 2: 8x8, 64 The Number of parameters is approx the same as Table 6 of [a]: ResNet20 0.27M ResNet32 0.46M ResNet44 0.66M ResNet56 0.85M ResNet110 1.7M # Arguments input_shape (tensor): shape of input image tensor depth (int): number of core convolutional layers num_classes (int): number of classes (CIFAR10 has 10) # Returns model (Model): Keras model instance """ if (depth - 2) % 6 != 0: raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])') # Start model definition. num_filters = 16 num_res_blocks = int((depth - 2) / 6) inputs = Input(shape=input_shape) x = resnet_layer(inputs=inputs) # Instantiate the stack of residual units for stack in range(3): for res_block in range(num_res_blocks): strides = 1 if stack > 0 and res_block == 0: # first layer but not first stack strides = 2 # downsample y = resnet_layer(inputs=x, num_filters=num_filters, strides=strides) y = resnet_layer(inputs=y, num_filters=num_filters, activation=None) if stack > 0 and res_block == 0: # first layer but not first stack # linear projection residual shortcut connection to match # changed dims x = resnet_layer(inputs=x, num_filters=num_filters, kernel_size=1, strides=strides, activation=None, batch_normalization=False) x = keras.layers.add([x, y]) x = Activation('relu')(x) num_filters *= 2 # Add classifier on top. # v1 does not use BN after last shortcut connection-ReLU x = AveragePooling2D(pool_size=8)(x) y = Flatten()(x) outputs = Dense(num_classes, activation='softmax', kernel_initializer='he_normal')(y) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model def resnet_v2(input_shape, depth, num_classes=10): """ResNet Version 2 Model builder [b] Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as bottleneck layer First shortcut connection per layer is 1 x 1 Conv2D. Second and onwards shortcut connection is identity. At the beginning of each stage, the feature map size is halved (downsampled) by a convolutional layer with strides=2, while the number of filter maps is doubled. Within each stage, the layers have the same number filters and the same filter map sizes. Features maps sizes: conv1 : 32x32, 16 stage 0: 32x32, 64 stage 1: 16x16, 128 stage 2: 8x8, 256 # Arguments input_shape (tensor): shape of input image tensor depth (int): number of core convolutional layers num_classes (int): number of classes (CIFAR10 has 10) # Returns model (Model): Keras model instance """ if (depth - 2) % 9 != 0: raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])') # Start model definition. num_filters_in = 16 num_res_blocks = int((depth - 2) / 9) inputs = Input(shape=input_shape) # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths x = resnet_layer(inputs=inputs, num_filters=num_filters_in, conv_first=True) # Instantiate the stack of residual units for stage in range(3): for res_block in range(num_res_blocks): activation = 'relu' batch_normalization = True strides = 1 if stage == 0: num_filters_out = num_filters_in * 4 if res_block == 0: # first layer and first stage activation = None batch_normalization = False else: num_filters_out = num_filters_in * 2 if res_block == 0: # first layer but not first stage strides = 2 # downsample # bottleneck residual unit y = resnet_layer(inputs=x, num_filters=num_filters_in, kernel_size=1, strides=strides, activation=activation, batch_normalization=batch_normalization, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_in, conv_first=False) y = resnet_layer(inputs=y, num_filters=num_filters_out, kernel_size=1, conv_first=False) if res_block == 0: # linear projection residual shortcut connection to match # changed dims x = resnet_layer(inputs=x, num_filters=num_filters_out, kernel_size=1, strides=strides, activation=None, batch_normalization=False) x = keras.layers.add([x, y]) num_filters_in = num_filters_out # Add classifier on top. # v2 has BN-ReLU before Pooling x = BatchNormalization()(x) x = Activation('relu')(x) x = AveragePooling2D(pool_size=8)(x) y = Flatten()(x) outputs = Dense(num_classes, activation='softmax', kernel_initializer='he_normal')(y) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model if version == 2: model = resnet_v2(input_shape=input_shape, depth=depth) else: model = resnet_v1(input_shape=input_shape, depth=depth) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=lr_schedule(0)), metrics=['accuracy']) model.summary() print('model_type', model_type) # Prepare model model saving directory. save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'cifar10_%s_model.{epoch:03d}.h5' % model_type if not os.path.isdir(save_dir): os.makedirs(save_dir) filepath = os.path.join(save_dir, model_name) # Prepare callbacks for model saving and for learning rate adjustment. checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_acc', verbose=1, save_best_only=True) lr_scheduler = LearningRateScheduler(lr_schedule) lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1), cooldown=0, patience=5, min_lr=0.5e-6) callbacks = [checkpoint, lr_reducer, lr_scheduler] # Run training, with or without data augmentation. if not data_augmentation: print('Not using data augmentation.') model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True, callbacks=callbacks) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( # set input mean to 0 over the dataset featurewise_center=False, # set each sample mean to 0 samplewise_center=False, # divide inputs by std of dataset featurewise_std_normalization=False, # divide each input by its std samplewise_std_normalization=False, # apply ZCA whitening zca_whitening=False, # epsilon for ZCA whitening zca_epsilon=1e-06, # randomly rotate images in the range (deg 0 to 180) rotation_range=0, # randomly shift images horizontally width_shift_range=0.1, # randomly shift images vertically height_shift_range=0.1, # set range for random shear shear_range=0., # set range for random zoom zoom_range=0., # set range for random channel shifts channel_shift_range=0., # set mode for filling points outside the input boundaries fill_mode='nearest', # value used for fill_mode = "constant" cval=0., # randomly flip images horizontal_flip=True, # randomly flip images vertical_flip=False, # set rescaling factor (applied before any other transformation) rescale=None, # set function that will be applied on each input preprocessing_function=None, # image data format, either "channels_first" or "channels_last" data_format=None, # fraction of images reserved for validation (strictly between 0 and 1) validation_split=0.0) # Compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=len(x_train) / batch_size, validation_data=(x_test, y_test), epochs=epochs, verbose=1, workers=4, callbacks=callbacks) # Score trained model. scores = model.evaluate(x_test, y_test, verbose=1) print('Test loss:', scores[0]) print('Test accuracy:', scores[1]) # Save Keras model tmp_model_path = os.path.join(args.output_dir, "tmp") if os.path.isdir(tmp_model_path): shutil.rmtree(tmp_model_path) os.mkdir(tmp_model_path) keras_model_path = os.path.join(tmp_model_path, 'keras_model.h5') model.save(keras_model_path) print('keras_model_path:', keras_model_path) # Convert Keras model to Tensorflow SavedModel def export_h5_to_pb(path_to_h5, export_path): # Set the learning phase to Test since the model is already trained. K.set_learning_phase(0) # Load the Keras model keras_model = load_model(path_to_h5) # Build the Protocol Buffer SavedModel at 'export_path' builder = saved_model_builder.SavedModelBuilder(export_path) # Create prediction signature to be used by TensorFlow Serving Predict API signature = predict_signature_def( inputs={"input_1": keras_model.input}, outputs={"dense_1": keras_model.output}) with K.get_session() as sess: # Save the meta graph and the variables # https://www.tensorflow.org/tfx/serving/serving_basic builder.add_meta_graph_and_variables( sess=sess, tags=[tag_constants.SERVING], signature_def_map={ signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature }) builder.save() tf_model_path = os.path.join(args.output_dir, "tf_saved_model") if os.path.isdir(tf_model_path): shutil.rmtree(tf_model_path) export_h5_to_pb(keras_model_path, tf_model_path) print('tf_model_path:', tf_model_path) print('saved_model_dir:', tf_model_path) # Apply TF_TRT on the Tensorflow SavedModel graph = tf.Graph() with graph.as_default(): with tf.Session(): # Create a TensorRT inference graph from a SavedModel: trt_graph = trt.create_inference_graph( input_graph_def=None, outputs=None, input_saved_model_dir=tf_model_path, input_saved_model_tags=[tag_constants.SERVING], max_batch_size=batch_size, max_workspace_size_bytes=2 << 30, precision_mode='FP16') print([n.name + '=>' + n.op for n in trt_graph.node]) tf.io.write_graph(trt_graph, os.path.join(model_dir, args.model_version), 'model.graphdef', as_text=False) # Remove tmp dirs # shutil.rmtree(tmp_model_path) # shutil.rmtree(tf_model_path) with open('/output.txt', 'w') as f: f.write(args.output_dir) print('input_dir: {}'.format(args.input_dir)) print('output_dir: {}'.format(args.output_dir))
i=0 result=0 val_loss_min=list() for train_index, test_index in kf.split(x, y): x_train=x[train_index] x_test=x[test_index] y_train=y[train_index] y_test=y[test_index] i+=1 print(str(n) + ' 번째 중 ' + str(i) + ' 번째 훈련') # x_train, x_val, y_train, y_val=train_test_split(x_train, y_train, train_size=0.9, random_state=99) train=datagen.flow(x_train, y_train, batch_size=64) val=datagen2.flow(x_test, y_test) test=datagen2.flow(x_test, y_test) pred2=datagen2.flow(pred, shuffle=False) es=EarlyStopping(monitor='val_loss', patience=100, mode='auto') rl=ReduceLROnPlateau(monitor='val_loss', patience=20, mode='auto', verbose=1, factor=0.1) cp=ModelCheckpoint(save_best_only=True, monitor='val_acc', mode='auto', filepath='../data/modelcheckpoint/weight.h5', verbose=1) cp2=ModelCheckpoint(filepath='../data/modelcheckpoint/weight_%s_{val_acc:.4f}_{val_loss:.4f}.hdf5'%i, save_best_only=True, monitor='val_acc', mode='auto') model=Sequential() model.add(Conv2D(256, 3, padding='same', input_shape=(28, 28, 1))) model.add(BatchNormalization()) model.add(Activation('relu'))
def train_kfold(self): #training parameters batch_size = self.batch_size learning_rate = self.learning_rate # get the kfolds kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=5) histories = [] i = 0 for train_index, test_index in kfold.split(self.x_train, self.y_train): i += 1 x_train = self.x_train[train_index] y_train = self.y_train[train_index] x_test = self.x_train[test_index] y_test = self.y_train[test_index] y_train = keras.utils.to_categorical(y_train, self.num_classes) y_test = keras.utils.to_categorical(y_test, self.num_classes) data_gen = ImageDataGenerator( rescale=1. / 255, horizontal_flip= True, # horizontal flip is the only custom augmentation preprocessing_function=preprocess_input) test_gen = ImageDataGenerator( rescale=1. / 255, preprocessing_function=preprocess_input) data_gen.fit(x_train) test_gen.fit(x_test) model = self.build_model() # compile the model sgd = optimizers.SGD(lr=learning_rate, momentum=0.9) model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"]) # early stopping fold_path = self.model_path.replace('.h5', '') + '_fold{}.h5'.format(i) early = EarlyStopping(monitor='val_accuracy', patience=10, verbose=1, mode='auto') checkpoint = ModelCheckpoint(fold_path, monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto') historytemp = model.fit_generator( data_gen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs=self.maxepoches, validation_data=test_gen.flow(x_test, y_test, shuffle=False), callbacks=[checkpoint, early], validation_steps=x_test.shape[0] // batch_size) historytemp = historytemp.__dict__ del historytemp['model'] histories.append(historytemp) _ = np.save(self.model_path.replace('.h5', '_kfolds.npy'), histories)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy']) checkpoint_save_path = "./checkpoint/cifar10.ckpt" if os.path.exists(checkpoint_save_path + '.index'): print('-------------load the model-----------------') model.load_weights(checkpoint_save_path) cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path, save_weights_only=True, # monitor='loss', save_best_only=True, verbose=2) history = model.fit(image_gen_train.flow(x_train, y_train, batch_size=128), epochs=200, validation_data=(x_test, y_test), validation_freq=1, callbacks=[cp_callback], verbose=1) model.summary() file = open('./weights.txt', 'w') # 参数提取 for v in model.trainable_variables: file.write(str(v.name) + '\n') file.write(str(v.shape) + '\n') file.write(str(v.numpy()) + '\n') file.close() ############################################### show ############################################### # 显示训练集和验证集的acc和loss曲线
def train_segmentation_model(img_dir): import os from glob import glob import random # from tqdm import tqdm from datetime import datetime random.seed(42) import numpy as np import matplotlib.pyplot as plt import tensorflow.keras.backend as K # from sklearn.model_selection import train_test_split from skimage.io import imread from skimage.transform import resize import tensorflow as tf # tf.executing_eagerly() from tensorflow.keras.preprocessing.image import load_img, img_to_array from tensorflow.keras.utils import to_categorical, Sequence from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping from tensorflow.keras.preprocessing.image import ImageDataGenerator HEIGHT = 224 WIDTH = 224 IMG_SHAPE = (HEIGHT, WIDTH, 3) def upsample(filters, size, norm_type='batchnorm', apply_dropout=False): initializer = tf.random_normal_initializer(0., 0.02) result = tf.keras.Sequential() result.add( tf.keras.layers.Conv2DTranspose(filters, size, strides=2, padding='same', kernel_initializer=initializer, use_bias=False)) if norm_type.lower() == 'batchnorm': result.add(tf.keras.layers.BatchNormalization()) elif norm_type.lower() == 'instancenorm': result.add(InstanceNormalization()) if apply_dropout: result.add(tf.keras.layers.Dropout(0.3)) result.add(tf.keras.layers.ReLU()) return result from tensorflow.keras.applications.mobilenet_v2 import preprocess_input def unet_model(): base_model = tf.keras.applications.MobileNetV2( input_shape=[*IMG_SHAPE], include_top=False) # Use the activations of these layers layer_names = [ 'block_1_expand_relu', # 64x64 'block_3_expand_relu', # 32x32 'block_6_expand_relu', # 16x16 'block_13_expand_relu', # 8x8 'block_16_project', # 4x4 ] layers = [base_model.get_layer(name).output for name in layer_names] # Create the feature extraction model down_stack = tf.keras.Model(inputs=base_model.input, outputs=layers) down_stack.trainable = False # for layer in down_stack.layers[-5:]: # layer.trainable = True up_stack = [ upsample(512, 3), # 4x4 -> 8x8 upsample(256, 3), # 8x8 -> 16x16 upsample(128, 3), # 16x16 -> 32x32 upsample(64, 3), # 32x32 -> 64x64 ] inputs = tf.keras.layers.Input(shape=[*IMG_SHAPE]) x = inputs # Downsampling through the model skips = down_stack(x) x = skips[-1] skips = reversed(skips[:-1]) # Upsampling and establishing the skip connections for up, skip in zip(up_stack, skips): x = up(x) concat = tf.keras.layers.Concatenate() x = concat([x, skip]) # This is the last layer of the model last = tf.keras.layers.Conv2DTranspose( 1, 3, strides=2, padding='same', activation='sigmoid') # 64x64 -> 128x128 x = last(x) return tf.keras.Model(inputs=inputs, outputs=x) def iou(label, pred): pred = pred > 0.5 label = label > 0.5 intersection = tf.reduce_sum(tf.cast(pred & label, tf.float32)) union = tf.reduce_sum(tf.cast(pred | label, tf.float32)) return intersection / tf.maximum(union, 1e-8) def dice_coef(y_true, y_pred, smooth=1): """ Dice = (2*|X & Y|)/ (|X|+ |Y|) = 2*sum(|A*B|)/(sum(A^2)+sum(B^2)) ref: https://arxiv.org/pdf/1606.04797v1.pdf """ intersection = K.sum(K.abs(y_true * y_pred), axis=-1) return (2. * intersection + smooth) / ( K.sum(K.square(y_true), -1) + K.sum(K.square(y_pred), -1) + smooth) def dice_coef_loss(y_true, y_pred): return 1 - dice_coef(y_true, y_pred) model = unet_model() model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=[iou]) train_datagen = ImageDataGenerator() test_datagen = ImageDataGenerator() def get_data(): train_dir = img_dir train_img_path = os.path.join(train_dir, 'images') train_gt_path = os.path.join(train_dir, 'gt') train_img_list = sorted(glob(os.path.join(train_img_path, '**/*.jpg'))) train_mask_list = sorted(glob(os.path.join(train_gt_path, '**/*.png'))) test_dir = img_dir[:-5] + 'test' test_img_path = os.path.join(test_dir, 'images') test_gt_path = '00_test_val_gt' test_img_list = sorted(glob(os.path.join(test_img_path, '**/*.jpg'))) test_mask_list = sorted(glob(os.path.join(test_gt_path, '**/*.png'))) pairs = [[img, mask] for img, mask in zip(train_img_list, train_mask_list)] print('Train len: ', len(pairs)) random.shuffle(pairs) X_train = [] y_train = [] for i, pair in enumerate(pairs): img = imread(pair[0]) label = imread(pair[1]) # img = resize(img, (HEIGHT, WIDTH)) label = resize(label, (HEIGHT, WIDTH)) # img = np.array(tf.image.resize(img, (HEIGHT, WIDTH))) # label = np.array(tf.image.resize(label[..., np.newaxis], (HEIGHT, WIDTH))) if len(img.shape) == 2: img = np.stack([img, img, img], axis=2) if len(label.shape) != 2: label = label[..., 0] X_train.append(img) y_train.append(label) if i % 100 == 0: print(i) pairs = [[img, mask] for img, mask in zip(test_img_list, test_mask_list)] print('Val len: ', len(pairs)) X_val = [] y_val = [] for i, pair in enumerate(pairs): img = imread(pair[0]) label = imread(pair[1]) img = resize(img, (HEIGHT, WIDTH)) label = resize(label, (HEIGHT, WIDTH)) # img = np.array(tf.image.resize(img, (HEIGHT, WIDTH))) # label = np.array(tf.image.resize(label[..., np.newaxis], (HEIGHT, WIDTH))) if len(img.shape) == 2: img = np.stack([img, img, img], axis=2) if len(label.shape) != 2: label = label[..., 0] X_val.append(img) y_val.append(label) if i % 100 == 0: print(i) return np.array(X_train), np.array(y_train), np.array(X_val), np.array( y_val) X_train, y_train, X_val, y_val = get_data() # X_train = preprocess_input(X_train) # X_val = preprocess_input(X_val) train_datagen.fit(X_train) learning_rate_reduction = ReduceLROnPlateau(monitor='val_iou', mode='max', patience=7, verbose=1, factor=0.5, min_lr=0.000000001) early_stop = EarlyStopping(monitor='val_iou', mode='max', patience=30, restore_best_weights=True) checkpoint = ModelCheckpoint('segmentation_model.hdf5', monitor='val_iou', mode='max', verbose=1, save_best_only=True) BATCH = 16 EPOCHS = 10000 model.fit(train_datagen.flow(X_train, y_train, seed=123, batch_size=BATCH), epochs=EPOCHS, validation_data=test_datagen.flow(X_val, y_val, seed=123, batch_size=BATCH), callbacks=[learning_rate_reduction, early_stop, checkpoint], workers=6) return model
model.add(layers.Dense(10, activation='softmax')) model.compile(optimizer='adam', loss=losses.sparse_categorical_crossentropy, metrics=['accuracy']) model.fit(X_train, train_labels, validation_data=(X_test, test_labels), epochs=20, batch_size=40) test_loss, test_acc = model.evaluate(X_test, test_labels, verbose=2) print('Accuracy on test set:', test_acc) datagen = ImageDataGenerator(height_shift_range=3, horizontal_flip=True) model_aug = tf.keras.models.clone_model(model) model_aug.compile(optimizer='adam', loss=losses.sparse_categorical_crossentropy, metrics=['accuracy']) train_generator = datagen.flow(X_train, train_labels, seed=42, batch_size=40) model_aug.fit(train_generator, epochs=50, validation_data=(X_test, test_labels)) test_loss, test_acc = model_aug.evaluate(X_test, test_labels, verbose=2) print('Accuracy on test set:', test_acc)
loss="categorical_crossentropy", metrics=["accuracy"]) train_datagen = ImageDataGenerator(featurewise_center=False, featurewise_std_normalization=False, zca_whitening=False, rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.1, horizontal_flip=False, vertical_flip=False, fill_mode='nearest') history1 = model.fit_generator(train_datagen.flow(X_train, Y_train, batch_size=batch_size), epochs=2, validation_data=(X_val, Y_val), verbose=2, steps_per_epoch=X_train.shape[0] // batch_size, callbacks=[learning_rate_reduction, ckpt]) sub = pd.read_csv("../input/digit-recognizer/sample_submission.csv") def predict_test(data, model): data = data / 255.0 data = data.values.reshape(-1, 28, 28, 1) return (model.predict(data))
model.add(Conv2D(256, (3, 3), padding="same")) model.add(Activation("relu")) model.add(BatchNormalization(axis=dim)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(1024)) model.add(Activation("relu")) model.add(BatchNormalization()) model.add(Dropout(0.5)) model.add(Dense(2)) model.add(Activation("sigmoid")) epochs = 100 ## compile the model opt = Adam(lr=lr) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) ## fit the model h = model.fit_generator(aug.flow(x_train, y_train, batch_size=batch_size), validation_data=(x_test, y_test), steps_per_epoch=len(x_train) // batch_size, epochs=epochs, verbose=1) ## save the model model.save('gender_predictor.model')
save_best_only=True, mode='max') # Log the epoch detail into csv csv_logger = CSVLogger(modelname + '.csv') callbacks_list = [checkpoint, csv_logger, LRScheduler] # Fit the model datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, rotation_range=20, horizontal_flip=True, vertical_flip=False) model.fit_generator( datagen.flow(trDat, trLbl, batch_size=12), validation_data=(tsDat, tsLbl), epochs=2, #originally 200 verbose=1, steps_per_epoch=len(trDat) / 12, callbacks=callbacks_list) # Now the training is complete, we get # another object to load the weights # compile it, so that we can do # final evaluation on it modelGo.load_weights(filepath) modelGo.compile(loss='categorical_crossentropy', optimizer=optmz, metrics=['accuracy'])
def train(): """ # [deprecated, cause of the program will be run on colab, input params will be specified] # 建立命令列程式 ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required=True, help="path to input dataset (i.e., directory of images)") ap.add_argument("-m", "--model", required=True, help="path to output model") ap.add_argument("-l", "--labelbin", required=True, help="path to output label binarizer") ap.add_argument("-p", "--plot", type=str, default="plot.png", help="path to output accuracy/loss plot") args = vars(ap.parse_args()) """ # 假設已在 Colab 中 cd 到專案主目錄 args = dict() #args.setdefault("dataset", "dataset") args.setdefault("dataset", r"dataset\test_dataset") # test args.setdefault("model", "fashion.model") args.setdefault("labelbin", "mlb.pickle") args.setdefault("plot", "plt.png") """ # 初始化參數 INIT_LR: initial learning rate BS: Batch Size IMG_DIMS: 讀入圖片尺寸 """ #EPOCHS = 75 EPOCHS = 3 # test INIT_LR = 1e-3 BS = 32 IMG_DIMS = (96, 96, 3) # (height, width, depth) tf.compat.v1.disable_eager_execution() # 從 dataset 命令參數的圖片路徑載入圖片 print("[INFO] 正在載入圖片...") img_paths = sorted(list(paths.list_images(args["dataset"]))) #print(*(img_path for img_path in img_paths), "\n") print(f"[INFO] 共有 {len(img_paths)} 張圖片") random.seed(42) random.shuffle(img_paths) # 初始化 圖片資料、標籤 串列 data = list() labels = list() # [預處理] 遍歷所有圖片,對所有圖片(feature)和所屬標籤(label)做預處理 # (1) 讀取所有圖片和標籤 for img_path in img_paths: # 載入圖片,做預處理後存入 data 串列 img = cv2.imread(img_path) if img is not None: # 若圖片非已損毀 # [USAGE] cv2. resize(img, (customized_width, customized_height)) img = cv2.resize(img, (IMG_DIMS[1], IMG_DIMS[0])) img = img_to_array(img) data.append(img) # 抽取出每張圖片的標籤(=>'(顏色)_(分類)')並加入 labels 串列 lbl = img_path.split(os.path.sep)[-2].split("_")[1:] labels.append(lbl) # (2) 特徵(圖片)縮放至 [0, 1] 區間 data = np.array(data, dtype="float") / 255.0 labels = np.array(labels) # list => numpy array print(f"[INFO] 共尋獲 {len(img_paths)} 張圖片") print(f"資料大小: {round(data.nbytes/(1024*1024), 2)} MB") # (3) 將文字標籤二值化為數值 (利用 sklearn 的 MultiLabelBinarizer) mlb = MultiLabelBinarizer() labels = mlb.fit_transform(labels) print("[INFO] 服飾分類標籤:") print(*(f"({i+1}) {label}" for i, label in enumerate(mlb.classes_))) # [訓練集/測試集拆分] 80%: 訓練集 / 20% 測試集 (trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.2, random_state=42) # 建構圖片生成器(image generator) aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") # 為了做"多標籤分類"(非多元分類),輸出層的 activation func. 選擇用 'sigmoid' 而非 'softmax' print("[INFO] 正在建立模型") model = SmallerVGGNet.build(width=IMG_DIMS[1], height=IMG_DIMS[0], depth=IMG_DIMS[2], n_classes=len(mlb.classes_), output_activation="sigmoid") # 設定優化器 (SGD 也足夠了) optimizer_ = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) # compile the model using binary cross-entropy rather than # categorical cross-entropy -- this may seem counterintuitive for # multi-label classification, but keep in mind that the goal here # is to treat each output label as an independent Bernoulli # distribution model.compile(loss="binary_crossentropy", optimizer=optimizer_, metrics=["accuracy"]) # 開始訓練神經網路 print("[INFO] 正在訓練神經網路") training_history = model.fit(x=aug.flow(trainX, trainY, batch_size=BS), validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS, epochs=EPOCHS, verbose=1) print("[INFO] 訓練結束!") # 儲存(serializing) 訓練好的模型到本地 print("[INFO] 正在儲存模型") model.save(args["model"], save_format="h5") # 儲存(serializing) 數值化的標籤到本地 print("[INFO] 正在儲存數值化的標籤") with open(args["labelbin"], "wb") as fp: fp.write(pickle.dumps(mlb)) # 繪製出: 訓練過程的 損失(loss) 及 準確率(accuracy) plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, EPOCHS), training_history.history["loss"], label="train_loss") plt.plot(np.arange(0, EPOCHS), training_history.history["val_loss"], label="val_loss") plt.plot(np.arange(0, EPOCHS), training_history.history["accuracy"], label="train_acc") plt.plot(np.arange(0, EPOCHS), training_history.history["val_accuracy"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend(loc="upper left") plt.savefig(args["plot"])
# set function that will be applied on each input preprocessing_function=None, # image data format, either "channels_first" or "channels_last" data_format=None, # fraction of images reserved for validation (strictly between 0 and 1) validation_split=0.0) # Compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), validation_data=(x_test, y_test), epochs=epochs, verbose=1, workers=4, callbacks=callbacks) #---------------------------------------------------------- data = x_test num_batches = data.shape[0] // batch_size for batch_idx in range(num_batches): start = batch_idx * batch_size
def create_augmented_images(*, external_generator, augm_img_nr=10, paramsforgenerator=""): """ Function that takes pictures in a batch, provided with keras generators and uses another generator. Secondarly, this function can be used to create dataframe with data on images in image batch if, augm_img_nr is set 0, external_generator : iterator, based on keras image generator the function was designed to work with all images in a given dataset provided as one batch, augm_img_nr : the number of augment images that will be created for each image, if augm_img_nr=0, no augmented images will be created, but both array, and dataframe will be returned, paramsforgenerator : dictionary, with parameters for image generator, used for image augmentation, Returns : numpy array with img batch, [?, pixel_size, pixel_size, 3] pandas dataframe, with rows corresponding to each image in the batch, and following columns: class = foldername in data directory, imagename= original image name, imgtype={'raw', 'aug'}, imgidnumber=0, foir raw, >=1 for augmented images """ # extract one batch with all images in a given dataset img_batch, batch_labels = next(external_generator) #.. create df, with class, image and image type names """ I will use this df, to create, new file with subdirectories, and save raw and augmented images with proper names """ img_filenames = pd.Series(external_generator.filenames).str.split( pat="/", expand=True) img_filenames = pd.concat([ img_filenames, pd.Series(["raw"] * img_filenames.shape[0]), pd.Series([0] * img_filenames.shape[0]) ], axis=1) img_filenames.columns = ["classname", "imgname", "imgtype", "imgidnumber"] # in case, I just wish to use that function to get everythign in the same format, but not to generate augmented images if augm_img_nr == 0: pass if augm_img_nr > 0: # Create generator for image augmentation datagen = ImageDataGenerator(**paramsforgenerator) datagen.fit(img_batch) #.. prepare iterator, that will return all figures in a batch, one by one, # augm_datagen.fit(img_batch) datagen_iter = datagen.flow(img_batch, batch_size=1, shuffle=False) # Create n augmented figures for each image in gthe batch, aug_img_filenames = list() for i in range(augm_img_nr): for j in range(img_batch.shape[0]): # create augmented figure, and add to new batch one_img = datagen_iter.next() if i + j == 0: batch_img_augm = one_img else: batch_img_augm = np.r_[batch_img_augm, one_img] # save name and id for that image aug_img_filenames.append({ "classname": img_filenames.iloc[j, 0], "imgname": img_filenames.iloc[j, 1], "imgtype": "aug", "imgidnumber": i + 1 }) # create new batch and df with labels and filenames to return, img_filenames = pd.concat( [img_filenames, pd.DataFrame(aug_img_filenames)], axis=0, sort=False).reset_index(drop=True) img_batch = np.r_[img_batch, batch_img_augm] #print(img_filenames.shape, img_batch.shape) return img_batch, img_filenames
# initialize the total number of images generated thus far print(len(datanp)) aug = ImageDataGenerator( rotation_range=30, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest") total = 0 imageGen=[] # construct the actual Python generator print("[INFO] generating images...") # augmentedImages=[] for i in range(len(datanp)): print("Generating is running") imageGen= aug.flow(datanp[i], batch_size=1, save_to_dir=out, save_prefix="image", save_format="jpg") for image in imageGen: # increment our counter total += 1 # if we have reached the specified number of examples, break # from the loop if (total%6==0): #total = 0 break
plt.figure(figsize=(10,10)) for c in range(100): plt.subplot(10,10,c + 1) plt.axis('off') plt.imshow(x_augment[c].reshape(28,28),cmap='gray') plt.show() ''' augment_size = 30000 randidx = np.random.randint(x_train.shape[0], size=augment_size) x_augment = x_train['randidx'].copy() y_augment = y_train[randidx.copy()] x_augment = img_generate.flow(x_augment, np.zeros(augment_size), batch_size=augment_size, shuffle=False).next()[0] # 원래 x_train에 image augment된 x_augment 를 추가 x_train = np.concatenate((x_train, x_augment)) y_train = np.concatenate((y_train, y_augment)) print(x_train.shape, ' ', y_train.shape) model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), input_shape=(28, 28, 1), padding='same', activation='relu'), tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
# initialize the optimizer and model print("[INFO] compiling model...") opt = SGD(lr=config.INIT_LR, momentum=0.9, decay=config.INIT_LR / config.NUM_EPOCHS) model = FireDetectionNet.build(width=128, height=128, depth=3, classes=2) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # check to see if we are attempting to find an optimal learning rate # before training for the full number of epochs if args["lr_find"] > 0: # initialize the learning rate finder and then train with learning # rates ranging from 1e-10 to 1e+1 print("[INFO] finding learning rate...") lrf = LearningRateFinder(model) lrf.find(aug.flow(trainX, trainY, batch_size=config.BATCH_SIZE), 1e-10, 1e+1, stepsPerEpoch=np.ceil( (trainX.shape[0] / float(config.BATCH_SIZE))), epochs=20, batchSize=config.BATCH_SIZE, classWeight=classWeight) # plot the loss for the various learning rates and save the # resulting plot to disk lrf.plot_loss() plt.savefig(config.LRFIND_PLOT_PATH) # gracefully exit the script so we can adjust our learning rates # in the config and then train the network for our full set of
# using early stopping to exit training if validation loss is not decreasing even after certain epochs (patience) earlystopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=20) # save the best model with lower validation loss checkpointer = ModelCheckpoint(filepath="FacialExpression_weights.hdf5", verbose=1, save_best_only=True) # In[71]: history = model_2_emotion.fit(train_datagen.flow(X_train, y_train, batch_size=64), validation_data=(X_val, y_val), steps_per_epoch=len(X_train) // 64, epochs=2, callbacks=[checkpointer, earlystopping]) # In[72]: # saving the model architecture to json file for future use model_json = model_2_emotion.to_json() with open("FacialExpression-model.json", "w") as json_file: json_file.write(model_json) # In[75]:
def train(self, model): #training parameters batch_size = 128 maxepoches = 250 learning_rate = 0.1 lr_decay = 1e-6 lr_drop = 20 # The data, shuffled and split between train and test sets: (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train, x_test = self.normalize(x_train, x_test) y_train = keras.utils.to_categorical(y_train, self.num_classes) y_test = keras.utils.to_categorical(y_test, self.num_classes) def lr_scheduler(epoch): return learning_rate * (0.5**(epoch // lr_drop)) reduce_lr = keras.callbacks.LearningRateScheduler(lr_scheduler) #data augmentation datagen = ImageDataGenerator( 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 rotation_range= 15, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) #optimization details sgd = optimizers.SGD(lr=learning_rate, decay=lr_decay, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) # training process in a for loop with learning rate drop every 25 epoches. historytemp = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs=maxepoches, validation_data=(x_test, y_test), callbacks=[reduce_lr], verbose=2) model.save_weights('cifar10vgg.h5') return model
episodes_history = [] for epc, epoch in enumerate(range(epoch)): print('Epoch', epc) for eps, episode in enumerate(episodes): print('======================================================') print('Episode', eps) print('======================================================') X_example, y_example, X_query_train, y_query_train, X_query_test, y_query_test = episode X_example = tf.constant(X_example, dtype=tf.float32) y_example = tf.constant(y_example, dtype=tf.float32) n_batch = 1 datagen.fit(X_query_train) for X_batch, y_batch in datagen.flow(X_query_train, y_query_train, batch_size=batch_size): with tf.GradientTape() as t: Z = features(X_example) ZT = tf.transpose(Z) try: ZZTlI_inv = tf.linalg.inv( tf.matmul(Z, ZT) + l * tf.eye(n_way * n_shot)) except: print('CANNOT TAKE INV') continue W = tf.matmul(tf.matmul(ZT, ZZTlI_inv), y_example) X_batch = tf.Variable(X_batch, dtype=tf.float32) y_batch = tf.Variable(y_batch, dtype=tf.float32)
# the actual model we will train) model = Model(inputs=baseModel.input, outputs=headModel) # loop over all layers in the base model and freeze them so they will # *not* be updated during the first training process for layer in baseModel.layers: layer.trainable = False # compile our model print("[INFO] compiling model...") opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the head of the network print("[INFO] training head...") H = model.fit(aug.flow(trainX, trainY, batch_size=BS), steps_per_epoch=len(trainX) // BS, validation_data=(testX, testY), validation_steps=len(testX) // BS, epochs=EPOCHS) # make predictions on the testing set print("[INFO] evaluating network...") predIdxs = model.predict(testX, batch_size=BS) # for each image in the testing set we need to find the index of the # label with corresponding largest predicted probability predIdxs = np.argmax(predIdxs, axis=1) # show a nicely formatted classification report print(
#img = load_img(imgfile, target_size=(256,256)) imgarray = img_to_array(img) orgs.append(imgarray) img = load_img(files_mask[file_num]) #print(np.array(img).shape) img = expand2square(img, (0, 0, 0)) img = img.resize((256, 256)) #img = load_img(imgfile, target_size=(256,256)) imgarray_mask = img_to_array(img) masks.append(imgarray_mask) seed = np.random.randint(1, 1000) img1 = masks[-1] img2 = orgs[-1] for i, data in enumerate(mask_datagen.flow(img1[np.newaxis, :, :, :], y=None, batch_size=1, shuffle=False, seed=seed)): data = cv2.cvtColor(data[0], cv2.COLOR_BGR2GRAY) masks_augment = np.append(masks_augment, data) if i == 4: break for i, data in enumerate(image_datagen.flow(img2[np.newaxis, :, :, :], y=None, batch_size=1, shuffle=False, seed=seed)): data = cv2.cvtColor(data[0], cv2.COLOR_BGR2GRAY) org_augment = np.append(org_augment, data) if i == 4: break for i in num_list: org_img = '/media/koshiba/Data/pix2pix/input/synthetic/' + img_name + '_' + str(i) + '.jpg' print(org_img) img = Image.open(org_img) img = expand2square(img, (0, 0, 0))
generator = ImageDataGenerator(rotation_range=15, width_shift_range=5. / 32, height_shift_range=5. / 32) generator.fit(trainX, seed=0) model = ResidualOfResidual(depth=40, width=2, dropout_rate=0.0, weights=None) optimizer = Adam(lr=1e-3) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc']) print('Finished compiling') checkpoint = callbacks.ModelCheckpoint('weights/RoR-WRN-40-2-Weights.h5', monitor='val_acc', save_best_only=True, save_weights_only=True) model.fit_generator(generator.flow(trainX, trainY, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size, epochs=epochs, callbacks=[checkpoint], validation_data=(testX, testY), verbose=2) scores = model.evaluate(testX, testY, batch_size) print('Test loss : ', scores[0]) print('Test accuracy : ', scores[1])
def create_image_data_generator(x, y, batch_size, rescale=None, rotation_range=None, width_shift_range=None, height_shift_range=None, shear_range=None, zoom_range=None, horizontal_flip=None, vertical_flip=None, brightness_range=None, save_to_dir=None, seed=42): """ Create image data generator for tensorflow Parameters ------------ x : np.ndarray or os.path X features. Either direct as numpy array or as os.path which will then be loaded y : np.ndarray or os.path Y labels. Either direct as numpy array or as os.path which will then be loaded """ # create image data generator img_args = {} # convert arguments to dictionary when not None if rescale is not None: img_args['rescale'] = rescale if rotation_range is not None: img_args['rotation_range'] = rotation_range if width_shift_range is not None: img_args['width_shift_range'] = width_shift_range if height_shift_range is not None: img_args['height_shift_range'] = height_shift_range if shear_range is not None: img_args['shear_range'] = shear_range if zoom_range is not None: img_args['zoom_range'] = zoom_range if horizontal_flip is not None: img_args['horizontal_flip'] = horizontal_flip if vertical_flip is not None: img_args['vertical_flip'] = vertical_flip if brightness_range is not None: img_args['brightness_range'] = brightness_range # create save_to_dir folder if not None if save_to_dir is not None: create_directory(save_to_dir) # create ImageDataGenerator from unpacked dictionary image_data_generator = ImageDataGenerator(**img_args) # check if x is numpy array, if not, then load x x = x if type(x) is np.ndarray else np.load(x) # same for y y = y if type(y) is np.ndarray else np.load(y) # create the generator generator = image_data_generator.flow(x=x, y=y, batch_size=batch_size, seed=seed, save_to_dir=save_to_dir) return generator