def __init__(self, input_size, normalizer, isTraining): single_input = Input(shape=[64, 64, 3]) encoder_inputs = Input(shape=input_size) # create the encoderblocks: x = single_input for i in range(4): x = enc_block_leaky(x, int(64 * 2**i), i + 1, normalizer, isTraining) # bottleneck of the encoder: x = Conv2D(512, 3, activation=None, padding='same', strides=1, name='encoder_conv5_1')(x) x = normalize(x, 'encoder_norm5_1', normalizer, isTraining) x = LeakyReLU()(x) x = Conv2D(512, 3, activation=None, padding='same', strides=1, name='encoder_conv5_2')(x) x = normalize(x, 'encoder_norm5_2', normalizer, isTraining) enc_out = LeakyReLU(name='bottleneck_relu_layer')(x) # create the encoder, which is the same for every snapshot encoder_model = Model(inputs=single_input, outputs=enc_out) # encode each snapshot individually through the same encoder (so weights are shared across the snapshots) encoded_img_list = [] index = 1 for i in range(0, input_size[2], 3): x = Lambda(lambda x: x[:, :, :, i:i + 3], name='img_{}'.format(str(index)))(encoder_inputs) encoded_img_list.append(encoder_model(x)) index += 1 # concatenate the encodings x = Concatenate(axis=3, name='conc_img_features')(encoded_img_list) # global convolutions for i in range(2): x = Conv2D(int(2048 / 2 ** i), 3, activation=None, padding='same', strides=1, name='{}_decoder_conv{}_{}'.format("conc_conv", index, i + 2))(x) x = normalize(x, '{}_decoder_norm{}_{}'.format("conc_conv", index, i + 2), 'instance', True) x = LeakyReLU()(x) # decoder blocks for i in range(5): x = big_dec_block_leaky(x, int(1024 / 2 ** i), i + 1, normalizer, isTraining, "D2") out = Conv2D(3, 3, activation='tanh', padding='same', name='final_conv')(x) self.stitchdecoder = Model(inputs=encoder_inputs, outputs=out, name='stitcher') self.stitchdecoder.summary() # enable multi-gpu-processing encoder_model = multi_gpu_model(encoder_model, gpus=2) self.stitchdecoder = multi_gpu_model(self.stitchdecoder, gpus=2) self.stitchdecoder.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss=vgg_loss, metrics=['accuracy', stitched_PSNR, stitched_ssim, perceptual_stitched_loss, mae_loss])
def compile_model(self): # init summary writer for tensorboard self.callback1 = TensorBoard(self.log_dir + '/discriminator') self.callback2 = TensorBoard(self.log_dir + '/generator') self.callback3 = TensorBoard(self.log_dir + '/generated_images') # model stuff input_shape = [self.image_size, self.image_size, self.image_channels] adam1 = Adam(lr=self.lr) adam2 = Adam(lr=self.lr * 2) # init and add multi-gpu support try: self.discriminator = multi_gpu_model(self.discriminator(), gpus=self.gpu) except: self.discriminator = self.discriminator() try: self.generator = multi_gpu_model(self.generator(), gpus=self.gpu) except: self.generator = self.generator() # compile discriminator self.discriminator.compile(loss='binary_crossentropy', optimizer=adam1) # compile generator input_tensor = Input(shape=input_shape) generated_catroon_tensor = self.generator(input_tensor) self.discriminator.trainable = False # for here we only train the generator discriminator_output = self.discriminator(generated_catroon_tensor) self.train_generator = Model( input_tensor, outputs=[generated_catroon_tensor, discriminator_output]) # add multi-gpu support try: self.train_generator = multi_gpu_model(self.train_generator, gpus=self.gpu) except: pass self.train_generator.compile( loss=[self.vgg_loss, 'binary_crossentropy'], loss_weights=[float(self.weight), 1.0], optimizer=adam2) # set callback model self.callback1.set_model(self.discriminator) self.callback2.set_model(self.train_generator) self.callback3.set_model(self.train_generator)
def _load_model(folder, model, weights_only=True): latest = os.path.join(folder, "latest") if model is None and weights_only: with open(os.path.join(folder, 'model.yaml'), 'r') as yaml_file: loaded_model_yaml = yaml_file.read() model = model_from_yaml(loaded_model_yaml, custom_objects={'tf': tf}) print('Model loaded from %s' % os.path.join(folder, 'model.yaml')) if os.path.isfile(latest): with open(latest, 'r') as f: filename = f.readlines()[0] epoch = filename.split('_')[1] # If model and weights were stored separately if weights_only: try: model.load_weights(os.path.join(folder, '%s.h5' % filename)) except: print('Single gpu loading failed, try with multi-gpu loading...') from tensorflow.python.keras.utils import multi_gpu_model multi_model = multi_gpu_model(model, gpus=len(os.environ["CUDA_VISIBLE_DEVICES"].split(',')) - 1) multi_model.load_weights(os.path.join(folder, '%s.h5' % filename)) model = multi_model.layers[-2] print('Weights loaded from %s' % os.path.join(folder, '%s.h5' % filename)) elif not weights_only: model = load_model(os.path.join(folder, '%s.h5' % filename), compile=False) print('Model loaded from %s' % os.path.join(folder, '%s.h5' % filename)) return model, int(epoch) return model, 0
def generate(self, model_path): num_anchors = len(self.anchors) num_classes = len(self.class_names) # self.yolo_model = load_model(model_path, compile=False) self.yolo_model = yolo_body( Input(shape=(None, None, self.num_channels)), num_anchors // 3, num_classes) self.yolo_model.load_weights(model_path) hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) np.random.seed(10101) # Fixed seed for consistent colors across runs. np.random.shuffle( self.colors) # Shuffle colors to decorrelate adjacent classes. np.random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. self.input_image_shape = K.placeholder(shape=(2, )) if self.gpu_num >= 2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num) boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes
def toMultiGpuModel(model): try: gpu_model = multi_gpu_model(model) return gpu_model except: print("gpu_model error") return None
def model_placement(model, num_gpus): """ Function to place model on correct device based on number of GPUs. Input: - model: model to fit. Model class using Keras Functional API. - num_gpus: Number of GPUs. 0 for CPU mode. Returns: - p_model: model placed on appropriate device. """ if num_gpus > 1: from tensorflow.python.keras.utils import multi_gpu_model with tf.device('/cpu:0'): p_model = model parallel_model = multi_gpu_model(p_model, gpus=num_gpus) return parallel_model elif num_gpus == 0: os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "" p_model = model return p_model else: with tf.device('/gpu:0'): p_model = model return p_model
def main(argv): with set_session().as_default(): (X_train, y_train), (X_val, y_val) = load_imdb(max_features=FLAGS.max_features, maxlen=FLAGS.maxlen) basemodel = BaseModel(input_dim=FLAGS.max_features, maxlen=FLAGS.maxlen) model = multi_gpu_model(basemodel.build(name=FLAGS.model_name), gpus=len(get_available_gpus())) model.compile(loss='binary_crossentropy', optimizer=FLAGS.optimizer, metrics=['accuracy']) model.fit( X_train.todense(), y_train, validation_data=(X_val.todense(), y_val), batch_size=FLAGS.batch_size, epochs=FLAGS.epochs, callbacks=[ ModelCheckpointToGcs( remotekey=FLAGS.gcs_path, filename= 'model_checkpoint.{epoch:02d}_{loss:.6f}_{val_loss:.6f}.h5', save_best_only=True), CSVLoggerToGcs(remotekey=FLAGS.gcs_path, filename=path.join('history_{}_{}.tsv').format( FLAGS.model_name, FLAGS.optimizer), separator='\t'), ])
def main(): os.makedirs(OPTS.models_dir, exist_ok=True) with open(os.path.join(OPTS.input_dir, 'sequences.json'), 'r') as f: sequences = json.loads(f.read()) with open(os.path.join(OPTS.input_dir, 'id2token.json'), 'rb') as f: id2token = json.loads(f.read().decode('utf-8')) with open(os.path.join(OPTS.models_dir, 'id2token.json'), 'wb') as fw: f.seek(0) fw.write(f.read()) print('lexicon:', len(id2token)) print('sequences:', len(sequences)) seq_min_len = min([len(seq) for seq in sequences]) print('seq_min_len:', seq_min_len) seq_max_len = max([len(seq) for seq in sequences]) print('seq_max_len:', seq_max_len) data_len = len(sequences) - len(sequences) % OPTS.batch_size data = sequences[0:data_len] data = pad_sequences(data, maxlen=seq_max_len, dtype='int32') model = create_model(seq_len=data.shape[-1], n_input_nodes=len(id2token), n_embedding_nodes=OPTS.embedding_size, n_hidden_nodes=OPTS.hidden_size, batch_size=OPTS.batch_size, stateful=False) ckpt_file = os.path.join(OPTS.models_dir, 'ckpt.h5') if os.path.exists(ckpt_file): model = load_model(ckpt_file) x = data y = np.roll(x, -1, 1) y = y[:, :, None] print('available devices:') print(device_lib.list_local_devices()) if OPTS.gpus > 1: model = multi_gpu_model(model, OPTS.gpus) save_step_callback = SaveStepCallback( model, save_every_batch=OPTS.save_every_batch) ckpt_callback = ModelCheckpoint(ckpt_file, monitor='loss', verbose=1, save_best_only=True, mode='min') model.fit(x=x, y=y, epochs=OPTS.epochs, initial_epoch=OPTS.initial_epoch, batch_size=OPTS.batch_size, callbacks=[save_step_callback, ckpt_callback], shuffle=True) model.save_weights(os.path.join(OPTS.models_dir, 'weights.h5'))
def add_predictor(self, side, model): """ Add a predictor to the predictors dictionary """ logger.debug("Adding predictor: (side: '%s', model: %s)", side, model) if self.gpus > 1: logger.debug("Converting to multi-gpu: side %s", side) model = multi_gpu_model(model, self.gpus) self.predictors[side] = model if not self.state.inputs: self.store_input_shapes(model)
def __init__(self, input_size, norm='batch', isTraining=None): """ A convolutional autoencoder which is used with random crops of 64x64x3. The bottleneck is 4x4x512 and leakyRelu is used all over the autoencoder. """ inputs = Input(shape=input_size, name='encoder_input') x = inputs # encoder part: for i in range(4): x = enc_block_leaky(x, int(64 * 2**i), i + 1, norm, isTraining) # bottleneck: x = Conv2D(512, 3, activation=None, padding='same', strides=1, name='encoder_conv5_1')(x) x = normalize(x, 'encoder_norm5_1', norm, isTraining) x = LeakyReLU()(x) x = Conv2D(512, 3, activation=None, padding='same', strides=1, name='encoder_conv5_2')(x) x = normalize(x, 'encoder_norm5_2', norm, isTraining) enc_out = LeakyReLU(name='bottleneck_relu_layer')(x) # decoder x = enc_out for i in range(4): x = dec_block_leaky(x, int(256 / 2**i), i + 1, norm, isTraining, "autoenc_v6") # final layer out = Conv2D(3, 3, activation='tanh', padding='same', strides=1, name='final_conv')(x) self.autoencoder = Model(inputs=inputs, outputs=out, name='autoencoder') self.autoencoder.summary() self.autoencoder = multi_gpu_model(self.autoencoder, gpus=2) self.autoencoder.compile( optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss=vgg_loss, metrics=['accuracy', PSNR, mae_loss, perceptual_loss])
def main(unused_argv): if platform.system() == 'Windows': print('Running on Windows') base_dir = os.path.join('E:\\', 'Program', 'Bite') save_path = os.path.join(base_dir, 'ckpt', 'weights-improvement-{epoch:02d}-{loss:.4f}-{acc:.2f}.hdf5') elif platform.system() == 'Linux': print('Running on Linux') base_dir = os.path.join('/media', 'md0', 'xt1800i', 'Bite') save_path = os.path.join(base_dir, 'ckpt', 'weights-improvement-{epoch:02d}-{loss:.4f}-{acc:.2f}.hdf5') else: print('Running on unsupported system') return tfrecord = os.path.join(base_dir, 'datasets', 'tfrecord', 'snake_all.tfrecord') with tf.device('/cpu:0'): model = keras_model() training_set = tfdata_generator(filename=tfrecord, batch_size=FLAGS.batch_size, aug=True) validation_set = tfdata_generator(filename=tfrecord, batch_size=FLAGS.batch_size) if FLAGS.ckpt is not None: print('loading weights') model.load_weights(FLAGS.ckpt) try: parallel_model = multi_gpu_model(model, gpus=2) print('Training on multiple GPUs') except: parallel_model = model print('Training on single GPU') global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(learning_rate=0.001, global_step=global_step, staircase=True, decay_steps=int(18288 / FLAGS.batch_size), decay_rate=0.96) # learning_rate = tf.keras.optimizers. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) print(model.summary()) parallel_model.compile(optimizer=optimizer, loss='categorical_crossentropy' , metrics=['accuracy']) ckpt = ModelCheckpoint(filepath=save_path, monitor='val_loss', save_best_only=False, save_weights_only=True, mode='auto', period=10) callbacks_list = [ckpt] parallel_model.fit(x=training_set.make_one_shot_iterator(), steps_per_epoch=int(18288 / FLAGS.batch_size), batch_size=FLAGS.batch_size, epochs=500000, validation_data=validation_set.make_one_shot_iterator(), validation_steps=int(18288 / FLAGS.batch_size), callbacks=callbacks_list)
def discriminator_model(self): if self.DM: return self.DM optimizer = RMSprop(lr=2e-4, decay=1e-8) self.DM = Sequential() self.DM.add(self.discriminator()) #multi_gpu with tf.device('/cpu:0'): self.DM = multi_gpu_model(self.DM, gpus=4) self.DM.compile(loss='binary_crossentropy', optimizer=optimizer,\ metrics=['accuracy']) return self.DM
def opt_init(self): opt = Adam(lr=self.lr, beta_1=0.9, beta_2=0.999, decay=0.01, epsilon=10e-8) if self.gpu_nums > 1: self.ctc_model = multi_gpu_model(self.ctc_model, gpus=self.gpu_nums) self.ctc_model.compile(loss={ 'ctc': lambda y_true, output: output }, optimizer=opt, metrics=['accuracy']) self.ctc_model.summary()
def generate(self): model_path = os.path.expanduser(self.model_path) assert model_path.endswith( '.h5'), 'Keras model or weights must be a .h5 file.' # Load model, or construct model and load weights. num_anchors = len(self.anchors) num_classes = len(self.class_names) is_tiny_version = num_anchors == 6 # default setting try: self.yolo_model = load_model(model_path, compile=False) except: self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \ if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes) self.yolo_model.load_weights( self.model_path) # make sure model, anchors and classes match else: assert self.yolo_model.layers[-1].output_shape[-1] == \ num_anchors/len(self.yolo_model.output) * (num_classes + 5), \ 'Mismatch between model and given anchor and class sizes' print('{} model, anchors, and classes loaded.'.format(model_path)) # Generate colors for drawing bounding boxes. hsv_tuples = [(x / len(self.class_names), 1., 1.) for x in range(len(self.class_names))] self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples)) self.colors = list( map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), self.colors)) np.random.seed(10101) # Fixed seed for consistent colors across runs. np.random.shuffle( self.colors) # Shuffle colors to decorrelate adjacent classes. np.random.seed(None) # Reset seed to default. # Generate output tensor targets for filtered bounding boxes. self.input_image_shape = K.placeholder(shape=(2, )) if self.gpu_num >= 2: self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num) boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors, len(self.class_names), self.input_image_shape, score_threshold=self.score, iou_threshold=self.iou) return boxes, scores, classes
def load(self, save_path): """ Load model weights, handles multi_gpu """ # multi gpu if self.n_gpu > 1: # cpu_merge used for nv_link : https://keras.io/utils/#multi_gpu_model # otherwise use : model = multi_gpu_model(model, cpu_relocation=True) # to train models with weights merge on CPU using cpu_relocation self.model = multi_gpu_model(self.model, gpus=self.n_gpu, cpu_merge=False) self.model.layers[-2].load_weights(save_path) else: self.model.load_weights(save_path) self.model.compile(optimizer=self.optimizer, loss='categorical_crossentropy', metrics=self.metrics) print(self.model.summary())
def fitness(learning_rate, num_dense_layers, num_epochs, num_conv_layers, kernel_size, num_filters): """ Hyper-parameters: learning_rate: Learning-rate for the optimizer. num_dense_layers: Number of dense layers. num_conv_layers: Number of nodes in each conv layer. kernel_size: kernel_size function for all layers. num_filters: Number of Filters """ # Print the hyper-parameters. print('learning rate: {0:.1e}'.format(learning_rate)) print('num_dense_layers:', num_dense_layers) print('num_conv_layers:', num_conv_layers) print('kernel_size:', kernel_size) print('num_filters:', num_filters) print() # Create the neural network with these hyper-parameters. model = create_model(learning_rate=learning_rate, num_dense_layers=num_dense_layers, num_epochs=num_epochs, num_conv_layers=num_conv_layers, kernel_size=kernel_size, num_filters=num_filters) # Dir-name for the TensorBoard log-files. log_dir = log_dir_name(learning_rate, num_dense_layers, num_conv_layers, kernel_size, num_filters) # Create a callback-function for Keras which will be # run after each epoch has ended during training. # This saves the log-files for TensorBoard. # Note that there are complications when histogram_freq=1. # It might give strange errors and it also does not properly # support Keras data-generators for the validation-set. callback_log = TensorBoard( log_dir=log_dir, batch_size=16, write_graph=True, write_grads=False, write_images=False) # Use Keras to train the model. #checker = torque[crossValidation:index] data_inputs = {} data_inputs['images'] = imageArray[0:crossValidation] data_inputs['roll'] = roll[0:crossValidation] data_inputs['pitch'] = pitch[0:crossValidation] parallel_model = multi_gpu_model(model, gpus=4) optimizer = Adam(lr=learning_rate, clipnorm=1.0, clipvalue=0.5) parallel_model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['mse']) history = parallel_model.fit(x=data_inputs, y=torque[0:crossValidation], epochs=num_epochs, batch_size=15, shuffle=False, validation_data=validation_data, callbacks=[callback_log]) # Get the classification accuracy on the validation-set # after the last training-epoch. accuracy = history.history['val_loss'][-1] # Print the classification accuracy. print() print("Error: {}".format(accuracy)) print() # Save the model if it improves on the best-found performance. # We use the global keyword so we update the variable outside # of this function. global best_accuracy # If the classification accuracy of the saved model is improved ... if accuracy < best_accuracy: # Save the new model to harddisk. model.save(path_best_model) # Update the classification accuracy. best_accuracy = accuracy # Delete the Keras model with these hyper-parameters from memory. del model # Clear the Keras session, otherwise it will keep adding new # models to the same TensorFlow graph each time we create # a model with a different set of hyper-parameters. K.clear_session() # NOTE: Scikit-optimize does minimization so it tries to # find a set of hyper-parameters with the LOWEST fitness-value. # Because we are interested in the HIGHEST classification # accuracy, we need to negate this number so it can be minimized. return accuracy
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "ihlmcwhbendw", [ "help", "input=", "learningrate=", "model=", "channel=", "width=", "height=", "batch=", "epoch=", "normalize=", "depth=", "weights=" ]) except getopt.GetoptError as err: print(str(err)) help() sys.exit(1) #Default Parmas CHANNEL = 3 WIDTH = 32 HEIGHT = 32 BATCH_SIZE = 32 LEARNING_RATE = 0.001 MODEL = "mlp" EPOCH = 1000 Gdalimg_path = '../data/training/' NORM_PATH = '../data/training/norm.txt' RESNET_DEPTH = 20 SAVE_PATH = 'saved_models/' + MODEL + "_" + str(LEARNING_RATE) + ".h5" for opt, arg in opts: if opt == "h" or opt == "--help": help() sys.exit(1) elif opt == "l" or opt == "--learningrate": LEARNING_RATE = float(arg) elif opt == "m" or opt == "--model": MODEL = arg elif opt == "c" or opt == "--channel": CHANNEL = int(arg) elif opt == "w" or opt == "--width": WIDTH = int(arg) elif opt == "h" or opt == "--height": HEIGHT = int(arg) elif opt == "b" or opt == "--batch": BATCH = int(arg) elif opt == "e" or opt == "--epoch": EPOCH = int(arg) elif opt == "i" or opt == "--input": Gdalimg_path = arg elif opt == "n" or opt == "--normalize": NORM_PATH = arg elif opt == "d" or opt == "--depth": RESNET_DEPTH = int(arg) elif opt == "w" or opt == "--weights": SAVE_PATH = arg + ".h5" (total_imgs, total_labels, num_categ) = GdalDataLoader(Gdalimg_path).getAllImages( CHANNEL, WIDTH, HEIGHT, True, False, True, NORM_PATH) print("NUMCATEG: " + str(num_categ)) total_labels = to_categorical(total_labels) if MODEL == "mlp": model = mlp_model(total_imgs.shape[1:], num_categ) elif MODEL == "simple_cnn": model = simple_cnn_model(total_imgs.shape[1:], num_categ) elif MODEL == "resnet": model = resnet_model(total_imgs.shape[1:], RESNET_DEPTH, num_categ) else: model = mlp_model(total_imgs.shape[1:], num_categ) model = multi_gpu_model(model, gpus=4) sgd = SGD(lr=LEARNING_RATE) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) model.fit(total_imgs, total_labels, epochs=EPOCH, batch_size=BATCH_SIZE, validation_split=0.1, shuffle=True, verbose=2) model.save(SAVE_PATH)
def transformer2(ih, iw, nb_conv, size_conv, nb_gpu=1): """ The simple cnn model for image transformation with 2 times of downsampling. It is a choice for fast running. However, it will lose resolution during the transformation. Parameters ---------- ih, iw : int The input image dimension nb_conv : int Number of convolution kernels for each layer size_conv : int The size of convolution kernel Returns ------- mdl Description. """ inputs = Input((ih, iw, 1)) conv1 = Conv2D(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(inputs) conv1 = Conv2D(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(pool1) conv2 = Conv2D(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(nb_conv * 4, (size_conv, size_conv), activation='relu', padding='same')(pool2) # fc1 = Flatten()(conv3) fc1 = Dense(iw * ih / 16)(fc1) fc1 = Reshape((ih // 4, iw // 4, 1))(fc1) conv4 = Conv2DTranspose(nb_conv * 4, (size_conv, size_conv), activation='relu', padding='same')(fc1) up1 = concatenate([UpSampling2D(size=(2, 2))(conv4), conv2], axis=3) conv6 = Conv2DTranspose(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(up1) conv6 = Conv2DTranspose(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(conv6) up2 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv1], axis=3) conv7 = Conv2DTranspose(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(up2) conv7 = Conv2DTranspose(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(conv7) conv8 = Conv2DTranspose(1, (size_conv, size_conv), activation='relu', padding='same')(conv7) mdl = Model(inputs=inputs, outputs=conv8) if nb_gpu > 1: mdl = multi_gpu_model(mdl, nb_gpu) mdl.compile(loss='mse', optimizer='Adam') return mdl
validation_data=validation_generator, epochs=epochs, use_multiprocessing=False, callbacks=callbacks, workers=4) # check weights weights = keras.backend.batch_get_value(model.weights) if all([np.all(w == ow) for w, ow in zip(weights, original_weights)]): print('Weights in the template model have not changed') else: print('Weights in the template model have changed') else: # 2 gpus original_weights = keras.backend.batch_get_value(model.weights) parallel_model = multi_gpu_model(model, gpus=gpus, cpu_relocation=False, cpu_merge=True) parallel_model.compile(optimizer=Adam(lr=1e-3), loss='mean_squared_error', metrics=[r_squared]) history = parallel_model.fit_generator( generator=training_generator, validation_data=validation_generator, epochs=epochs, use_multiprocessing=False, callbacks=[tensorboard], workers=4) # check weights # https://github.com/keras-team/keras/issues/11313 weights = keras.backend.batch_get_value(model.weights) parallel_weights = keras.backend.batch_get_value(parallel_model.weights)
vehicle = np.zeros_like(y) construction[y==2] = 255. human[y==6] = 255. vehicle[y==7] = 255. result = img.copy() alpha = 0.4 img[:,:,1] = construction img[:,:,2] = vehicle img[:,:,0] = human cv2.addWeighted(img, alpha, result, 1-alpha, 0, result) cv2.imwrite(f'outputs/{epoch}.png', cv2.cvtColor(result, cv2.COLOR_RGB2BGR)) print('Wrote file to disk') unet = DilatedNet(256, 256, 8,use_ctx_module=True, bn=True) p_unet = multi_gpu_model(unet, 4) p_unet.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[dice_coeff, 'accuracy']) tb = TensorBoard(log_dir='logs', write_graph=True) mc = ModelCheckpoint(mode='max', filepath='models-dr/pdilated.h5', monitor='acc', save_best_only='True', save_weights_only='True', verbose=1) es = EarlyStopping(mode='max', monitor='acc', patience=6, verbose=1) vis = visualize() callbacks = [tb, mc, es] train_gen = seg_gen(image_list, mask_list, batch_size) p_unet.fit_generator(train_gen, steps_per_epoch=steps, epochs=8, callbacks=callbacks, workers=8) print('Saving final weights') unet.save_weights('dilated.h5')
def run(self): tf.compat.v1.disable_eager_execution() # 입력 값을 받게 추가합니다. parser = argparse.ArgumentParser() parser.add_argument('--learning_rate', required=False, type=float, default=0.001) parser.add_argument('--dropout_rate', required=False, type=float, default=0.2) parser.add_argument('--batch_size', required=False, type=int, default=16) parser.add_argument('--epoch', required=False, type=int, default=10) # relu, sigmoid, softmax, tanh parser.add_argument('--act', required=False, type=str, default='relu') args = parser.parse_args() input = Input(shape=(200, 200, 3)) model = InceptionV3(input_tensor=input, include_top=False, weights='imagenet', pooling='max') for layer in model.layers: layer.trainable = False input_image_size = (200, 200) x = model.output x = Dense(1024, name='fully')(x) x = Dropout(args.dropout_rate)(x) x = BatchNormalization()(x) x = Activation(args.act)(x) x = Dense(512)(x) x = Dropout(args.dropout_rate)(x) x = BatchNormalization()(x) x = Activation(args.act)(x) x = Dense(101, activation='softmax', name='softmax')(x) model = Model(model.input, x) model.summary() train_datagen = ImageDataGenerator(rescale=1. / 255, validation_split=0.2) batch_size = args.batch_size train_generator = train_datagen.flow_from_directory( '/result/caltech101', target_size=input_image_size, batch_size=batch_size, class_mode='categorical', subset='training') validation_generator = train_datagen.flow_from_directory( '/result/caltech101', target_size=input_image_size, batch_size=batch_size, class_mode='categorical', subset='validation') model = multi_gpu_model(model, gpus=2) model.compile( optimizer=tf.keras.optimizers.Adam(lr=args.learning_rate), loss='categorical_crossentropy', metrics=['acc']) early_stopping = EarlyStopping(patience=20, mode='auto', monitor='val_acc') hist = model.fit_generator( train_generator, verbose=0, steps_per_epoch=train_generator.samples // batch_size, validation_data=validation_generator, epochs=args.epoch, callbacks=[early_stopping, KatibMetricLog()])
x = Conv2D(filters=512, kernel_size=(3, 3), strides=(1, 1), padding='same', name='block5_conv4')(x) x = BatchNormalization()(x) x = ReLU()(x) x = MaxPool2D(pool_size=(2, 2), strides=(2, 2))(x) x = GlobalAveragePooling2D()(x) x = Dense(units=53, activation='softmax', name='output_predictions')(x) model = Model(inputs=input_image, outputs=x, name='Classifier') # In[3]: parallel_model = multi_gpu_model(model=model, gpus=4) tb = TensorBoard(log_dir='logs', write_graph=True) mc = ModelCheckpoint(filepath='models/top_weights.h5', monitor='val_acc', save_best_only='True', save_weights_only='True', verbose=1) es = EarlyStopping(monitor='val_loss', patience=15, verbose=1) rlr = ReduceLROnPlateau() callbacks = [tb, mc, es, rlr] nadam = Nadam(lr=1e-3) parallel_model.compile(optimizer=nadam, loss='categorical_crossentropy', metrics=['accuracy']) # In[ ]:
def __init__(self, input_size, weights_path, normalizer, isTraining): encoder_inputs = Input(shape=input_size) # create the autoencoder, load the pre-trained weights autoenc = ConvAutoencoder([input_size[0], input_size[1], 3], norm="instance", isTraining=False) autoenc.autoencoder.load_weights(weights_path) # freeze the weights autoenc.isNotTraining() # create the encoder encoder_model = Model( inputs=autoenc.autoencoder.input, outputs=autoenc.autoencoder.get_layer('autoencoder').get_layer( name='bottleneck_relu_layer').output) encoder_model.trainable = False # encode each image individually through the pre-trained encoder encoded_img_list = [] index = 1 for i in range(0, input_size[2], 3): x = Lambda(lambda x: x[:, :, :, i:i + 3], name='img_{}'.format(str(index)))(encoder_inputs) encoded_img_list.append(encoder_model(x)) index += 1 # concatenate the encodings x = Concatenate(axis=3, name='conc_img_features')(encoded_img_list) # global convolutions for i in range(2): x = Conv2D(int(2048 / 2**i), 3, activation=None, padding='same', strides=1, name='{}_decoder_conv{}_{}'.format( "conc_conv", index, i + 2))(x) x = normalize( x, '{}_decoder_norm{}_{}'.format("conc_conv", index, i + 2), 'instance', True) x = LeakyReLU()(x) # decoder blocks for i in range(5): x = big_dec_block_leaky(x, int(1024 / 2**i), i + 1, normalizer, isTraining, "D2") out = Conv2D(3, 3, activation='tanh', padding='same', name='final_conv')(x) self.stitchdecoder = Model(inputs=encoder_inputs, outputs=out, name='stitcher') self.stitchdecoder.summary() # enable multi-gpu-processing self.stitchdecoder = multi_gpu_model(self.stitchdecoder, gpus=2) self.stitchdecoder.compile( optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss=vgg_loss, metrics=[ 'accuracy', stitched_PSNR, stitched_ssim, perceptual_stitched_loss, mae_loss ])
def train_on_texts(self, texts, context_labels=None, batch_size=128, num_epochs=50, verbose=1, new_model=False, gen_epochs=1, train_size=1.0, max_gen_length=300, validation=True, dropout=0.0, via_new_model=False, save_epochs=0, multi_gpu=False, **kwargs): if new_model and not via_new_model: self.train_new_model(texts, context_labels=context_labels, num_epochs=num_epochs, gen_epochs=gen_epochs, train_size=train_size, batch_size=batch_size, dropout=dropout, validation=validation, save_epochs=save_epochs, multi_gpu=multi_gpu, **kwargs) return if context_labels: context_labels = LabelBinarizer().fit_transform(context_labels) if 'prop_keep' in kwargs: train_size = prop_keep if self.config['word_level']: texts = [text_to_word_sequence(text, filters='') for text in texts] # calculate all combinations of text indices + token indices indices_list = [ np.meshgrid(np.array(i), np.arange(len(text) + 1)) for i, text in enumerate(texts) ] indices_list = np.block(indices_list) # If a single text, there will be 2 extra indices, so remove them # Also remove first sequences which use padding if self.config['single_text']: indices_list = indices_list[self.config['max_length']:-2, :] indices_mask = np.random.rand(indices_list.shape[0]) < train_size if multi_gpu: num_gpus = len(K.tensorflow_backend._get_available_gpus()) batch_size = batch_size * num_gpus gen_val = None val_steps = None if train_size < 1.0 and validation: indices_list_val = indices_list[~indices_mask, :] gen_val = generate_sequences_from_texts(texts, indices_list_val, self, context_labels, batch_size) val_steps = max( int(np.floor(indices_list_val.shape[0] / batch_size)), 1) indices_list = indices_list[indices_mask, :] num_tokens = indices_list.shape[0] assert num_tokens >= batch_size, "Fewer tokens than batch_size." level = 'word' if self.config['word_level'] else 'character' print("Training on {:,} {} sequences.".format(num_tokens, level)) steps_per_epoch = max(int(np.floor(num_tokens / batch_size)), 1) gen = generate_sequences_from_texts(texts, indices_list, self, context_labels, batch_size) base_lr = 4e-3 # scheduler function must be defined inline. def lr_linear_decay(epoch): return (base_lr * (1 - (epoch / num_epochs))) if context_labels is not None: if new_model: weights_path = None else: weights_path = "{}_weights.hdf5".format(self.config['name']) self.save(weights_path) self.model = textgenrnn_model(self.num_classes, dropout=dropout, cfg=self.config, context_size=context_labels.shape[1], weights_path=weights_path) model_t = self.model if multi_gpu: # Do not locate model/merge on CPU since sample sizes are small. parallel_model = multi_gpu_model(self.model, gpus=num_gpus, cpu_merge=False) parallel_model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=4e-3, rho=0.99)) model_t = parallel_model print("Training on {} GPUs.".format(num_gpus)) model_t.fit_generator(gen, steps_per_epoch=steps_per_epoch, epochs=num_epochs, callbacks=[ LearningRateScheduler(lr_linear_decay), generate_after_epoch(self, gen_epochs, max_gen_length), save_model_weights(self, num_epochs, save_epochs) ], verbose=verbose, max_queue_size=10, validation_data=gen_val, validation_steps=val_steps) # Keep the text-only version of the model if using context labels if context_labels is not None: self.model = Model(inputs=self.model.input[0], outputs=self.model.output[1])
def create_model(pretrained_weights=None, input_size=None): """ A fairly standard U-Net with skip connections and an increased receptive field """ inputs = Input(input_size) conv1 = Conv2D(64, 15, activation='relu', padding='same')(inputs) conv1 = Conv2D(64, 15, activation='relu', padding='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(128, 13, activation='relu', padding='same')(pool1) conv2 = Conv2D(128, 13, activation='relu', padding='same', dilation_rate=2)(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(256, 13, activation='relu', padding='same')(pool2) conv3 = Conv2D(256, 13, activation='relu', padding='same', dilation_rate=2)(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(512, 9, activation='relu', padding='same')(pool3) conv4 = Conv2D(512, 9, activation='relu', padding='same')(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = Conv2D(1024, 6, activation='relu', padding='same')(pool4) conv5 = Conv2D(1024, 3, activation='relu', padding='same')(conv5) up6 = Conv2DTranspose(512, 2, activation='relu', padding='same', strides=(2, 2))(conv5) merge6 = concatenate([conv4, up6], axis=3) conv6 = Conv2D(512, 3, activation='relu', padding='same')(merge6) conv6 = Conv2D(512, 3, activation='relu', padding='same')(conv6) up7 = Conv2DTranspose(256, 2, activation='relu', padding='same', strides=(2, 2))(conv6) merge7 = concatenate([conv3, up7], axis=3) conv7 = Conv2D(256, 3, activation='relu', padding='same')(merge7) conv7 = Conv2D(256, 3, activation='relu', padding='same')(conv7) up8 = Conv2DTranspose(128, 2, activation='relu', padding='same', strides=(2, 2))(conv7) merge8 = concatenate([conv2, up8], axis=3) conv8 = Conv2D(128, 3, activation='relu', padding='same')(merge8) conv8 = Conv2D(128, 3, activation='relu', padding='same')(conv8) up9 = Conv2DTranspose(64, 2, activation='relu', padding='same', strides=(2, 2))(conv8) merge9 = concatenate([conv1, up9], axis=3) conv9 = Conv2D(64, 3, activation='relu', padding='same')(merge9) conv9 = Conv2D(64, 3, activation='relu', padding='same')(conv9) up10 = Conv2DTranspose(32, 2, activation='relu', padding='same', strides=(2, 2))(conv9) conv10 = Conv2D(32, 3, activation='relu', padding='same')(up10) out = Conv2D(3, 3, activation='tanh', padding='same', strides=1)(conv10) model = Model(inputs=inputs, outputs=out) model = multi_gpu_model(model, gpus=2) model.compile(optimizer='adam', loss=l1_loss.custom_loss, metrics=['accuracy', stitched_PSNR, stitched_ssim]) model.summary() if pretrained_weights: model.load_weights(pretrained_weights) return model
drop = 0.5 epochs_drop = 3.0 lrate = initial_lrate * \ math.pow(drop, math.floor((1 + epoch) / epochs_drop)) print(f'Setting lr =>{lrate}') return lrate model = DeepLabV3Plus(img_height, img_width, classes) try: print('Loading previous weights') model.load_weights('top_weights.h5') except: print('Training from scratch !!!') print('*** Building multi_gpu_model ***') pmodel = multi_gpu_model(model, 4) pmodel.compile(optimizer=Adam(lr=1e-4), loss='sparse_categorical_crossentropy', metrics=['accuracy']) print('*** Building multi_gpu_model completed ***') tb = TensorBoard(log_dir='logs', write_graph=True) mc = ModelCheckpoint(mode='max', filepath='top_weights.h5', monitor='val_acc', save_best_only='True', save_weights_only='True', verbose=1) lrsched = LearningRateScheduler(lr_sched) viz = TensorBoardImage('validation_images') callbacks = [mc, viz, tb]
dnn_feature_columns = fixlen_feature_columns linear_feature_columns = fixlen_feature_columns feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns) # 3.generate input data for model train, test = train_test_split(data, test_size=0.2) train_model_input = {name: train[name] for name in feature_names} test_model_input = {name: test[name] for name in feature_names} # 4.Define Model,train,predict and evaluate model = DeepFM(linear_feature_columns, dnn_feature_columns, task='binary') model = multi_gpu_model(model, gpus=2) model.compile( "adam", "binary_crossentropy", metrics=['binary_crossentropy'], ) history = model.fit( train_model_input, train[target].values, batch_size=256, epochs=10, verbose=2, validation_split=0.2, )
def transformer3_pooling(ih, iw, nb_conv, size_conv, nb_gpu): """ The cnn image transformation model with 3 times of downsampling. The downsampling uses maxpooling. Parameters ---------- ih, iw : int The input image dimension nb_conv : int Number of convolution kernels for each layer size_conv : int The size of convolution kernel Returns ------- mdl Description. """ inputs = Input((ih, iw, 1)) conv1 = Conv2D(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(inputs) conv1 = Conv2D(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(pool1) conv2 = Conv2D(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(pool2) conv3 = Conv2D(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(nb_conv * 4, (size_conv, size_conv), activation='relu', padding='same')(pool3) conv4 = Conv2D(nb_conv * 4, (size_conv, size_conv), activation='relu', padding='same')(conv4) conv4 = Conv2D(1, (size_conv, size_conv), activation='relu', padding='same')(conv4) # fc1 = Flatten()(conv4) fc1 = Dense(iw * ih / 128, activation='relu')(fc1) fc1 = Dropout(0.2)(fc1) fc1 = Dense(iw * ih / 128, activation='relu')(fc1) fc1 = Dropout(0.25)(fc1) fc1 = Dense(iw * ih / 64, activation='relu')(fc1) fc1 = Dropout(0.25)(fc1) fc1 = Reshape((int(ih // 8), int(iw // 8), 1))(fc1) fc2 = Conv2DTranspose(nb_conv * 4, (size_conv, size_conv), activation='relu', padding='same')(fc1) fc2 = Conv2DTranspose(nb_conv * 8, (size_conv, size_conv), activation='relu', padding='same')(fc2) up1 = concatenate([UpSampling2D(size=(2, 2))(fc2), conv3], axis=3) conv6 = Conv2DTranspose(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(up1) conv6 = Conv2DTranspose(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(conv6) up2 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv2], axis=3) conv7 = Conv2DTranspose(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(up2) conv7 = Conv2DTranspose(nb_conv * 2, (size_conv, size_conv), activation='relu', padding='same')(conv7) up3 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv1], axis=3) conv8 = Conv2DTranspose(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(up3) conv8 = Conv2DTranspose(nb_conv, (size_conv, size_conv), activation='relu', padding='same')(conv8) conv8 = Conv2DTranspose(1, (3, 3), activation='relu', padding='same')(conv8) mdl = Model(inputs=inputs, outputs=conv8) if nb_gpu > 1: mdl = multi_gpu_model(mdl, nb_gpu) mdl.compile(loss='mse', optimizer='Adam') return mdl
def __init__(self, ser_model, gpus): pmodel = multi_gpu_model(ser_model, gpus) self.__dict__.update(pmodel.__dict__) self._smodel = ser_model
def train(parser): config = json.load(open('./setting.json')) d_num = parser.d_num im_size = 256 weight_save_dir = config['weight_save_dir'] weight_dir = os.path.join(weight_save_dir, str(d_num)) os.makedirs(weight_dir,exist_ok=True) weight_file=os.path.join(weight_dir,dt.today().strftime("%m%d")+'.h5') START_TIME = time.time() reporter = Reporter(parser=parser) loader = Loader(parser=parser) train_gen, valid_gen, test_gen = loader.return_gen(False) train_steps, valid_steps, test_steps = loader.return_step() # ---------------------------model---------------------------------- input_channel_count = parser.input_channel output_channel_count = 3 first_layer_filter_count = parser.filter if parser.eito: network = UNet8(input_channel_count, output_channel_count, first_layer_filter_count, im_size=im_size, parser=parser) else: network = UNet(input_channel_count, output_channel_count, first_layer_filter_count, im_size=im_size, parser=parser) model = network.get_model() if not ON_WIN: model = multi_gpu_model(model, gpus=2) optimizer = tf.keras.optimizers.Adam(lr=parser.trainrate) # model.compile(loss=[DiceLossByClass(im_size, 3).dice_coef_loss], optimizer=optimizer, metrics=[dice, dice_1, dice_2]) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=[dice, dice_1, dice_2]) model.summary() # ---------------------------training---------------------------------- batch_size = parser.batch_size epochs = parser.epoch config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.9 config.gpu_options.allow_growth = True sess = tf.Session(config=config) logdir = os.path.join('./logs', dt.today().strftime("%Y%m%d_%H%M")) os.makedirs(logdir, exist_ok=True) tb_cb = TensorBoard(log_dir=logdir, histogram_freq=0, write_graph=True, write_images=True) es_cb = EarlyStopping(monitor='val_loss', patience=parser.early_stopping, verbose=1, mode='auto') mc_cb = ModelCheckpoint(filepath=weight_file, monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='min', period=1) print('dnum is:',d_num) print("start training.") print(valid_steps) # Pythonジェネレータ(またはSequenceのインスタンス)によりバッチ毎に生成されたデータでモデルを訓練します. history = model.fit_generator( generator=train_gen, steps_per_epoch=train_steps, epochs=epochs, max_queue_size=10 if ON_WIN else 32, workers=1 if ON_WIN else WORKERS * gpu_count, use_multiprocessing=False, validation_steps=valid_steps, validation_data=valid_gen, # use_multiprocessing=True, callbacks=[es_cb, tb_cb,mc_cb]) print("finish training. And start making predict.") # ---------------------------predict---------------------------------- test_preds = model.predict_generator(test_gen, steps=test_steps, verbose=1) ELAPSED_TIME = int(time.time() - START_TIME) reporter.add_log_documents(f'ELAPSED_TIME:{ELAPSED_TIME} [sec]') # ---------------------------report---------------------------------- parser.save_logs = True if parser.save_logs: reporter.add_val_loss(history.history['val_loss']) reporter.add_val_dice(history.history['val_dice']) reporter.add_model_name(network.__class__.__name__) reporter.generate_main_dir() reporter.plot_history(history) reporter.save_params(history) train_gen, valid_gen, test_gen, output_gen_path = loader.return_gen(return_path=True) for i in range(min(train_steps, SAVE_BATCH_SIZE)): batch_input, batch_teach = next(train_gen) batch_preds = model.predict(batch_input) reporter.plot_predict(batch_input, batch_preds, batch_teach, 'train', batch_num=i) for i in range(min(valid_steps, SAVE_BATCH_SIZE)): batch_input, batch_teach = next(valid_gen) batch_preds = model.predict(batch_input) reporter.plot_predict(batch_input, batch_preds, batch_teach, 'valid', batch_num=i) for i in range(min(test_steps, SAVE_BATCH_SIZE)): batch_input, batch_teach = next(test_gen) batch_preds = model.predict(batch_input) reporter.plot_predict(batch_input, batch_preds, batch_teach, 'test', batch_num=i) if parser.output_predict: print('make output_predict') _, _, test_gen, output_gen_path = loader.return_gen(return_path=True) for i in range(test_steps): batch_output_path = next(output_gen_path) batch_input, batch_teach = next(test_gen) batch_preds = model.predict(batch_input) reporter.output_predict(batch_preds,batch_output_path,suffix=parser.suffix)