def load_model(self, model_id=0, weights_file='cifar10.hdf5'): if model_id == 0: img_input = Input(shape=self.input_shape) model = residual_network(img_input, self.num_classes, 5, self.weight_decay) elif model_id == 1: img_input = Input(shape=self.input_shape) model = residual_network(img_input, self.num_classes, 8, self.weight_decay) elif model_id == 2: self.batch_size = 64 model = wide_residual_network(self.input_shape, self.num_classes) elif model_id == 3: img_input = Input(shape=self.input_shape) model = residual_network(img_input, self.num_classes, 3, self.weight_decay) else: logger.fatal("unsupported model id") exit(2) # sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True) # model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) weights_file = os.path.join(self.script_path, weights_file) if not os.path.isfile(weights_file): logger.fatal( "You have not trained the model yet, please train model first." ) model.load_weights(weights_file) return model
def load_model(self, model_id=0, weights_file='models/kvasir.hdf5'): model = self.def_model(model_id) weights_file = os.path.join(self.script_path, weights_file) if not os.path.isfile(weights_file): logger.fatal( "You have not train the model yet, please train model first.") model.load_weights(weights_file) return model
def __init__(self, source_dir="kvasir-dataset", start_point=0, epoch=50): self.config = ExperimentalConfig.gen_config() # Config related to images in the gtsrb dataset self.start_point = start_point self.num_classes = 8 self.IMG_SIZE = 50 self.epoch = epoch self.name = "kvasir" self.batch_size = 128 self.input_shape = (self.IMG_SIZE, self.IMG_SIZE, 3) self.script_path = os.path.dirname(os.path.abspath(__file__)) self.root_dir = os.path.join(self.script_path, source_dir) if not os.path.isdir(self.root_dir): logger.fatal("Please download the dataset first.") prepare_date() __image_path = os.path.join(self.script_path, "endoscope_images1.npy") __label_path = os.path.join(self.script_path, "endoscope_labels1.npy") the_endoscope_images1, the_endoscope_labels1 = np.load(__image_path), \ np.load(__label_path) self.X_train, self.X_test, self.Y_train, self.Y_test = train_test_split( the_endoscope_images1, the_endoscope_labels1, test_size=0.2) # define default data generator self.datagen_rotation = 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 dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening zca_epsilon=1e-06, # epsilon for ZCA whitening rotation_range= 30, # randomly rotate images in the range (deg 0 to 180) width_shift_range=0.1, # randomly shift images horizontally height_shift_range=0.1, # randomly shift images vertically shear_range=0.2, # set range for random shear zoom_range=0., # set range for random zoom channel_shift_range=0., # set range for random channel shifts fill_mode= 'nearest', # set mode for filling points outside the input boundaries cval=0., # value used for fill_mode = "constant" horizontal_flip=True, # randomly flip images vertical_flip=False, # randomly flip images rescale= None, # set rescaling factor (applied before any other transformation) preprocessing_function= None, # set function that will be applied on each input data_format=None, # image data format, either "channels_first" or # "channels_last" validation_split=0.0 # fraction of images reserved for validation # (strictly between 0 and 1) )
def load_model(self, model_id=0, weights_file='cifar10.hdf5'): model = resnet_v1(input_shape=self.input_shape, depth=self.depth) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=lr_schedule(self.epoch)), metrics=['accuracy']) weights_file = os.path.join(self.script_path, weights_file) if not os.path.isfile(weights_file): logger.fatal("You have not train the model yet, please train model first.") model.load_weights(weights_file) return model
def __init__(self, data_path="data", start_point=0, epoch=30): self.script_path = os.path.dirname(os.path.abspath(__file__)) root_dir = os.path.join(self.script_path, data_path) if not os.path.isdir(root_dir): logger.fatal("Please download the dataset first.") exit(1) self.config = ExperimentalConfig.gen_config() # Config related to images in the gtsrb dataset self.start_point = start_point self.epoch = epoch # load data (self.x_train, self.y_train), (self.x_test, self.y_test) = self.load_svhn_data(root_dir) self.num_classes = 10 self.name = "svhn" self.batch_size = 64 self.image_size = 32 self.input_shape = (self.image_size, self.image_size, 3) print("running 1.5") self.datagen_rotation = 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 dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening zca_epsilon=1e-06, # epsilon for ZCA whitening rotation_range= 30, # randomly rotate images in the range (deg 0 to 180) width_shift_range=0.1, # randomly shift images horizontally height_shift_range=0.1, # randomly shift images vertically shear_range=0.2, # set range for random shear zoom_range=0., # set range for random zoom channel_shift_range=0., # set range for random channel shifts fill_mode= 'nearest', # set mode for filling points outside the input boundaries cval=0., # value used for fill_mode = "constant" horizontal_flip=True, # randomly flip images vertical_flip=False, # randomly flip images rescale= None, # set rescaling factor (applied before any other transformation) preprocessing_function= None, # set function that will be applied on each input data_format=None, # image data format, either "channels_first" or # "channels_last" validation_split=0.0 # fraction of images reserved for validation # (strictly between 0 and 1) )
def load_model(self, model_id=0, weights_file='cifar10.hdf5'): model = self.build_model() learning_rate = 0.1 lr_decay = 1e-6 sgd = optimizers.SGD(lr=learning_rate, decay=lr_decay, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) weights_file = os.path.join(self.script_path, weights_file) if not os.path.isfile(weights_file): logger.fatal( "You have not train the model yet, please train model first.") model.load_weights(weights_file) return model
def train_dnn_model(self, _model=None, x_train=None, y_train=None, x_val=None, y_val=None, train_strategy=None): """train a dnn model on cifar-10 dataset based on train strategy""" # k.set_image_data_format('channels_last') model_id = _model[0] weights_file = _model[1] weights_file = os.path.join(self.script_path, weights_file) checkpoint = ModelCheckpoint(weights_file, monitor='acc', verbose=1, save_best_only=False) callbacks_list = [LearningRateScheduler(self.scheduler), checkpoint] img_input = Input(shape=self.input_shape) if model_id == 0: model = residual_network(img_input, self.num_classes, 5, self.weight_decay) elif model_id == 1: model = residual_network(img_input, self.num_classes, 8, self.weight_decay) elif model_id == 2: self.batch_size = 64 model = wide_residual_network(self.input_shape, self.num_classes) change_lr = LearningRateScheduler(scheduler) callbacks_list.append(change_lr) elif model_id == 3: model = residual_network(img_input, self.num_classes, 3, self.weight_decay) else: logger.fatal("unsupported model id") exit(2) if self.start_point > 0: model.load_weights(weights_file) x_val = self.preprocess_original_imgs(x_val) if train_strategy is None: x_train = self.preprocess_original_imgs(x_train) self.datagen_rotation.fit(x_train) data = self.datagen_rotation.flow(x_train, y_train, batch_size=self.batch_size) # model.fit(x_train, y_train, # batch_size=self.batch_size, # epochs=self.epoch, # validation_data=(x_val, y_val), # shuffle=True, # callbacks=callbacks_list) else: graph = tf.get_default_graph() data = DataGenerator(self, model, x_train, y_train, self.batch_size, train_strategy, graph) model.fit_generator( data, steps_per_epoch=len(x_train) / self.batch_size, validation_data=(x_val, y_val), epochs=self.epoch, verbose=1, #workers=4, callbacks=callbacks_list) return model