def test1(): seq_size = 10 batch_size = 10 rnn_size = 1 xin = Input(batch_shape=(batch_size, seq_size,1)) xtop = Input(batch_shape=(batch_size, seq_size)) xbranch, xsummary = RTTN(rnn_size, return_sequences=True)([xin, xtop]) model = Model(input=[xin, xtop], output=[xbranch, xsummary]) model.compile(loss='MSE', optimizer='SGD') data_gen = generate_data_batch(batch_size, seq_size) model.fit_generator(generator=data_gen, samples_per_epoch=1000, nb_epoch=100)
predictions = Dense(12, activation='softmax')(x) model = Model(input=base_model.input, output=predictions) # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers for layer in base_model.layers: layer.trainable = False # compile the model (should be done *after* setting layers to non-trainable) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) model.fit_generator(train_generator, samples_per_epoch=20000, nb_epoch=10, callbacks=[TensorBoard(log_dir='/tmp/attempt1')]) # Save the top-only trained model. model.save('object_classifierfit1.h5') for i, layer in enumerate(base_model.layers): print(i, layer.name, "Input: ", layer.input_shape, "Output: ", layer.output_shape) # Unlcok the "bottom" of the model and lock the "top" for layer in model.layers[:172]: layer.trainable = False for layer in model.layers[172:]: layer.trainable = True
height_shift_range=0.1, shear_range=0., zoom_range=0., channel_shift_range=0., fill_mode='constant', cval=0., horizontal_flip=False, vertical_flip=False, dim_ordering='tf') datagen.fit(data_images_train) # Model saving callback checkpointer = ModelCheckpoint(filepath=WEIGHTS_SEGMENT_FILEPATH, verbose=1, save_best_only=True) # Early stopping early_stopping = EarlyStopping(monitor='val_loss', patience=20) history = model_segment.fit_generator( datagen.flow(data_images_train, data_masks_train, batch_size=batch_size), samples_per_epoch=data_images_train.shape[0], nb_epoch=nb_epoch, verbose=2, validation_data=(data_images_val, data_masks_val[:, :, :, 0]), callbacks=[checkpointer, early_stopping]) with open(HISTORY_SEGMENT_FILEPATH, 'w') as f_out: json.dump(history.history, f_out)
class FergusNModel(object): def __init__(self, igor): now = datetime.now() self.run_name = "fergusn_{}mo_{}day_{}hr_{}min".format( now.month, now.day, now.hour, now.minute) log_location = join(igor.log_dir, self.run_name + ".log") self.logger = igor.logger = make_logger(igor, log_location) igor.verify_directories() self.igor = igor @classmethod def from_yaml(cls, yamlfile, kwargs=None): igor = Igor.from_file(yamlfile) igor.prep() model = cls(igor) model.make(kwargs) return model @classmethod def from_config(cls, config, kwargs=None): igor = Igor(config) model = cls(igor) igor.prep() model.make(kwargs) return model def load_checkpoint_weights(self): weight_file = join(self.igor.model_location, self.igor.saving_prefix, self.igor.checkpoint_weights) if exists(weight_file): self.logger.info("+ Loading checkpoint weights") self.model.load_weights(weight_file, by_name=True) else: self.logger.warning( "- Checkpoint weights do not exist; {}".format(weight_file)) def plot(self): filename = join(self.igor.model_location, self.igor.saving_prefix, 'model_visualization.png') kplot(self.model, to_file=filename) self.logger.debug("+ Model visualized at {}".format(filename)) def make(self, theano_kwargs=None): '''Make the model and compile it. Igor's config options control everything. Arg: theano_kwargs as dict for debugging theano or submitting something custom ''' if self.igor.embedding_type == "convolutional": make_convolutional_embedding(self.igor) elif self.igor.embedding_type == "token": make_token_embedding(self.igor) elif self.igor.embedding_type == "shallowconv": make_shallow_convolutional_embedding(self.igor) elif self.igor.embedding_type == "minimaltoken": make_minimal_token_embedding(self.igor) else: raise Exception("Incorrect embedding type") B = self.igor.batch_size spine_input_shape = (B, self.igor.max_num_supertags) child_input_shape = (B, 1) parent_input_shape = (B, 1) E, V = self.igor.word_embedding_size, self.igor.word_vocab_size # for word embeddings repeat_N = self.igor.max_num_supertags # for lex mlp_size = self.igor.mlp_size ## dropout parameters p_emb = self.igor.p_emb_dropout p_W = self.igor.p_W_dropout p_U = self.igor.p_U_dropout w_decay = self.igor.weight_decay p_mlp = self.igor.p_mlp_dropout def predict_params(): return { 'output_dim': 1, 'W_regularizer': l2(w_decay), 'activation': 'relu', 'b_regularizer': l2(w_decay) } dspineset_in = Input(batch_shape=spine_input_shape, name='daughter_spineset_in', dtype='int32') pspineset_in = Input(batch_shape=spine_input_shape, name='parent_spineset_in', dtype='int32') dhead_in = Input(batch_shape=child_input_shape, name='daughter_head_input', dtype='int32') phead_in = Input(batch_shape=parent_input_shape, name='parent_head_input', dtype='int32') dspine_in = Input(batch_shape=child_input_shape, name='daughter_spine_input', dtype='int32') inputs = [dspineset_in, pspineset_in, dhead_in, phead_in, dspine_in] ### Layer functions ############# Convert the word indices to vectors F_embedword = Embedding(input_dim=V, output_dim=E, mask_zero=True, W_regularizer=l2(w_decay), dropout=p_emb) if self.igor.saved_embeddings is not None: self.logger.info("+ Cached embeddings loaded") F_embedword.initial_weights = [self.igor.saved_embeddings] ###### Prediction Functions ## these functions learn a vector which turns a tensor into a matrix of probabilities ### P(Parent supertag | Child, Context) F_parent_predict = ProbabilityTensor( name='parent_predictions', dense_function=Dense(**predict_params())) ### P(Leaf supertag) F_leaf_predict = ProbabilityTensor( name='leaf_predictions', dense_function=Dense(**predict_params())) ###### Network functions. ##### Input word, correct its dimensions (basically squash in a certain way) F_singleword = compose(Fix(), F_embedword) ##### Input spine, correct diemnsions, broadcast across 1st dimension F_singlespine = compose(RepeatVector(repeat_N), Fix(), self.igor.F_embedspine) ##### Concatenate and map to a single space F_alignlex = compose( RepeatVector(repeat_N), Dropout(p_mlp), Dense(mlp_size, activation='relu', name='dense_align_lex'), concat) F_alignall = compose( Distribute(Dropout(p_mlp), name='distribute_align_all_dropout'), Distribute(Dense(mlp_size, activation='relu', name='align_all_dense'), name='distribute_align_all_dense'), concat) F_alignleaf = compose( Distribute( Dropout(p_mlp * 0.66), name='distribute_leaf_dropout' ), ### need a separate oen because the 'concat' is different for the two situations Distribute(Dense(mlp_size, activation='relu', name='leaf_dense'), name='distribute_leaf_dense'), concat) ### embed and form all of the inputs into their components ### note: spines == supertags. early word choice, haven't refactored. leaf_spines = self.igor.F_embedspine(dspineset_in) pspine_context = self.igor.F_embedspine(pspineset_in) dspine_single = F_singlespine(dspine_in) dhead = F_singleword(dhead_in) phead = F_singleword(phead_in) ### combine the lexical material lexical_context = F_alignlex([dhead, phead]) #### P(Parent Supertag | Daughter Supertag, Lexical Context) ### we know the daughter spine, want to know the parent spine ### size is (batch, num_supertags) parent_problem = F_alignall( [lexical_context, dspine_single, pspine_context]) ### we don't have the parent, we just have a leaf leaf_problem = F_alignleaf([lexical_context, leaf_spines]) parent_predictions = F_parent_predict(parent_problem) leaf_predictions = F_leaf_predict(leaf_problem) predictions = [parent_predictions, leaf_predictions] theano_kwargs = theano_kwargs or {} ## make it quick so i can load in the weights. self.model = Model(input=inputs, output=predictions, preloaded_data=self.igor.preloaded_data, **theano_kwargs) #mask_cache = traverse_nodes(parent_prediction) #desired_masks = ['merge_3.in.mask.0'] #self.p_tensor = K.function(inputs+[K.learning_phase()], [parent_predictions, F_parent_predict.inbound_nodes[0].input_masks[0]]) if self.igor.from_checkpoint: self.load_checkpoint_weights() elif not self.igor.in_training: raise Exception("No point in running this without trained weights") if not self.igor.in_training: expanded_children = RepeatVector(repeat_N, axis=2)(leaf_spines) expanded_parent = RepeatVector(repeat_N, axis=1)(pspine_context) expanded_lex = RepeatVector(repeat_N, axis=1)( lexical_context ) # axis here is arbitary; its repeating on 1 and 2, but already repeated once huge_tensor = concat( [expanded_lex, expanded_children, expanded_parent]) densely_aligned = LastDimDistribute( F_alignall.get(1).layer)(huge_tensor) output_predictions = Distribute( F_parent_predict, force_reshape=True)(densely_aligned) primary_inputs = [phead_in, dhead_in, pspineset_in, dspineset_in] leaf_inputs = [phead_in, dhead_in, dspineset_in] self.logger.info("+ Compiling prediction functions") self.inner_func = K.Function(primary_inputs + [K.learning_phase()], output_predictions) self.leaf_func = K.Function(leaf_inputs + [K.learning_phase()], leaf_predictions) try: self.get_ptensor = K.function( primary_inputs + [K.learning_phase()], [ output_predictions, ]) except: import pdb pdb.set_trace() else: optimizer = Adam(self.igor.LR, clipnorm=self.igor.max_grad_norm, clipvalue=self.igor.grad_clip_threshold) theano_kwargs = theano_kwargs or {} self.model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=['accuracy'], **theano_kwargs) #self.model.save("here.h5") def likelihood_function(self, inputs): if self.igor.in_training: raise Exception("Not in testing mode; please fix the config file") return self.inner_func(tuple(inputs) + (0., )) def leaf_function(self, inputs): if self.igor.in_training: raise Exception("Not in testing mode; please fix the config file") return self.leaf_func(tuple(inputs) + (0., )) def train(self): replacers = { "daughter_predictions": "child", "parent_predictions": "parent", "leaf_predictions": "leaf" } train_data = self.igor.train_gen(forever=True) dev_data = self.igor.dev_gen(forever=True) N = self.igor.num_train_samples E = self.igor.num_epochs # generator, samplers per epoch, number epochs callbacks = [ProgbarV2(3, 10, replacers=replacers)] checkpoint_fp = join(self.igor.model_location, self.igor.saving_prefix, self.igor.checkpoint_weights) self.logger.info("+ Model Checkpoint: {}".format(checkpoint_fp)) callbacks += [ ModelCheckpoint(filepath=checkpoint_fp, verbose=1, save_best_only=True) ] callbacks += [LearningRateScheduler(lambda epoch: self.igor.LR * 0.9)] csv_location = join(self.igor.log_dir, self.run_name + ".csv") callbacks += [CSVLogger(csv_location)] self.model.fit_generator(generator=train_data, samples_per_epoch=N, nb_epoch=E, callbacks=callbacks, verbose=1, validation_data=dev_data, nb_val_samples=self.igor.num_dev_samples) def debug(self): dev_data = self.igor.dev_gen(forever=False) X, Y = next(dev_data) self.model.predict_on_batch(X) #self.model.evaluate_generator(dev_data, self.igor.num_dev_samples) def profile(self, num_iterations=1): train_data = self.igor.train_gen(forever=True) dev_data = self.igor.dev_gen(forever=True) # generator, samplers per epoch, number epochs callbacks = [ProgbarV2(1, 10)] self.logger.debug("+ Beginning the generator") self.model.fit_generator(generator=train_data, samples_per_epoch=self.igor.batch_size * 10, nb_epoch=num_iterations, callbacks=callbacks, verbose=1, validation_data=dev_data, nb_val_samples=self.igor.batch_size) self.logger.debug( "+ Calling theano's pydot print.. this might take a while") theano.printing.pydotprint(self.model.train_function.function, outfile='theano_graph.png', var_with_name_simple=True, with_ids=True) self.logger.debug("+ Calling keras' print.. this might take a while") self.plot("keras_graph.png")
inputs = pretrained.input outputs = pretrained.output print(inputs) print(outputs) hidden = Dense(128, activation='relu')(outputs) dropout = Dropout(.3)(hidden) preds = Dense(2, activation='softmax')(dropout) model = Model(inputs, preds) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['acc']) model.fit_generator(train_batches, epochs=50, validation_data=val_batches, steps_per_epoch=1105 // 4, validation_steps=255 // 4) # serialize model to JSON model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights("Dogs_vs_cats_model.h5") print("Saved model to disk") model.summary()
class Cnn_model: """ Klasa umożliwiająca detekcję twarzy z użyciem konwolucyjnych sieci neuronowych. Zawiera wszystkie dane wejściowe, obliczeniowe i model sieci służący do obliczeń. Dodatkowo zawiera funkcje umożliwiające dodanie nowej osoby do bazy oraz identyfikację osób. """ def __init__(self): # Prametry początkowe dla modelu self.label_id = [] self.img_data = [] self.index = 0 # Ilość epok równa 50, ilość klas do określenia przy dodawaniu osób (poprzez kolejne dodane osoby) self.nb_class = 0 self.epochs_num = 50 self.fotosperclass_number = 60 def initialize_networkmodel(self): """ Funkcja inicjalizuje model sieci. Model użyty to VGGFace bez trzech ostatnich warstw, które zostały zamienione na warstwy o 64, 32 neuronach, z ostatnią warstwą o ilości neuronów równej ilości klas. Następuje inicjalizacja parametrów sieci takich jak momentum czy wskaźnik uczenia się. Określane są metody ewaluacji precyzji i straty. """ ###### Struktura modelu ###### vgg_model = VGGFace(include_top=False, weights='vggface', input_shape=(180, 180, 3)) last_layer = vgg_model.get_layer('pool5').output # Add layers x = Flatten(name='flatten')(last_layer) x = Dense(64, activation='relu', name='fc6')(x) x = Dense(32, activation='relu', name='fc7')(x) out = Dense(self.nb_class, activation='softmax', name='fc8')(x) self.custom_vgg_model = Model(vgg_model.input, out) # Zamrożenie uczenia wszystkich warstw poza trzema ostatnimi, które zostały dodane layer_count = 0 for layer in self.custom_vgg_model.layers: layer_count = layer_count + 1 for l in range(layer_count - 3): self.custom_vgg_model.layers[l].trainable = False ###### Kompilacja modelu i parametrów uczenia (learning rate, momentum) ###### sgd = optimizers.SGD(lr=5e-3, decay=1e-6, momentum=0.9, nesterov=True) self.custom_vgg_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) def add_person(self, **kwargs): """ Funkcja umożliwia dodanie osoby do bazy do treningu sieci neuronowej. Jeśli parametr *gui* jest przekazany wizualizacja danych z kamery nastapi w aplikacji GUI. W innym wypadku wyświetlone zostanie okno z podglądem kamery. Args: **kwargs: gui: Obiekt głównej aplikacji GUI, dzięki któremu możliwa jest wizualizacja danych z kamery w aplikacji. """ # Sprawdzenie czy parametr został przekazany gui = kwargs.get('gui', False) # Przekazanie parametru gui do kolejnej funkcji, gdzie jeden z elementów gui zostanie wykorzystany data = face_recording(gui=gui, im_count=self.fotosperclass_number, cnn=True) # Jeśli dodawana jest pierwsza twarz if not self.label_id: curr_id = 0 # Jeśli twarz nie jest pierwsza else: curr_id = max(self.label_id) + 1 self.label_id.extend([curr_id] * len(data)) self.img_data.extend(data) self.nb_class += 1 def data_processing(self): """ Funkcja generuje batche zdjęć i dzieli je na zestawy do uczenia i do walidacji. Zestaw danych do uczenia jest powiększony przez generowane dodatkowo zdjęcia o różnych przekształceniach (rotacja, przesunięcia, odwrócenie zdjęcia). """ # Hot encoding - wymagana forma etykiet dla uczenia sieci self.hot_label_id = to_categorical(self.label_id) # Rozbicie danych na treningowe i do walidacji X_train, X_test, y_train, y_test = train_test_split(np.array( self.img_data), self.hot_label_id, test_size=0.2) train_datagen = ImageDataGenerator(rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, fill_mode='nearest') validation_datagen = ImageDataGenerator() # Ustawienie parametru batchsize równego długości danych treningowych podzielonych przez 10 % długości self.train_batchsize = int(round(len(X_train)) / 10) self.train_generator = train_datagen.flow( X_train, y_train, shuffle=True, batch_size=self.train_batchsize) self.validation_generator = validation_datagen.flow( X_test, y_test, batch_size=self.train_batchsize) def train_cnn(self): """ Funkcja odpowiada za trening konwolucyjnej sieci neuronowej. Do modelu przekazywane są zestawy zdjęć do uczenia i walidacji, a także następuje przekierowanie informacji nt. statusu uczenia sieci do oddzielnego strumienia. Umożliwia on wyświetlanie takiej informacji w aplikacji GUI. Dodatkowo dodany został Callback EarlyStopping, który w razie zmiany celności nie większej niż 1% - po 5 epokach bez zmiany skończy proces uczenia. """ # Stworzenie listy Callbacków - jeden odpowiada za wyświetlanie danych w GUI - drugi za zatrzymanie uczenia # w przypadku gdy przez 5 epok accuracy nie urośnie o więcej niż 1%. self.stream_output = OutputStream() early_stopping = EarlyStopping(monitor='acc', min_delta=0.01, patience=5, verbose=0, mode='max') callback_list = [early_stopping, self.stream_output] # Historia rejestrująca zmiany w procesie uczenia self.history = self.custom_vgg_model.fit_generator( self.train_generator, steps_per_epoch=self.train_generator.n / self.train_generator.batch_size, epochs=self.epochs_num, validation_data=self.validation_generator, validation_steps=self.validation_generator.n / self.validation_generator.batch_size, verbose=1, callbacks=callback_list) # # Zapisz model do pliku # self.custom_vgg_model.save('trained_model_{}'.format( # datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d_%H%M%S'))) def recognize_face(self, gui=False): """ Identyfikacja osoby z użyciem wcześniej wytrenowanej sieci neuronowej. Jeśli parametr *gui* jest dodany nastąpi wyświetlenie obrazu z kamery w aplikacji GUI, jeśli nie - pojawi się dodatkowe okno. Args: gui: Obiekt głównej aplikacji GUI, dzięki któremu możliwa jest wizualizacja danych z kamery w aplikacji. Returns: image: poszukiwana twarz (zdjęcie). model.predict: wartości prawdopodobieństwa przynależności do danej klasy. """ try: assert self.custom_vgg_model except: raise AttributeError() image = take_image(gui=gui, cnn=True) return image, self.custom_vgg_model.predict( np.expand_dims(image, axis=0))[0] def accuracy_statistics(self): """ Wyświetlenie danych dotyczących precyzji i straty w trakcie uczenia sieci neuronowej. Dane są podzielone na precyzję i stratę dla zestawu uczącego i zestawu walidacyjnego. """ ##### Pokaż wyniki uczenia ##### acc = self.history.history['acc'] val_acc = self.history.history['val_acc'] loss = self.history.history['loss'] val_loss = self.history.history['val_loss'] epochs = range(len(acc)) # Precyzja uczelnia plt.subplot(121) plt.plot(epochs, acc) plt.plot(epochs, val_acc) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') # "Strata" uczenia plt.subplot(122) plt.plot(epochs, loss) plt.plot(epochs, val_loss) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='upper left') plt.show()
'batch_size': 1, 'n_classes': 3, 'n_channels': 4, 'shuffle': True} # Generators print(len(train_val_test_dict['train'])) training_generator = SingleDataGenerator(train_val_test_dict['train'], **params) validation_generator = SingleDataGenerator(train_val_test_dict['val'], **params) cb_1=keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=2, verbose=0, mode='auto') cb_2=keras.callbacks.ModelCheckpoint(filepath="1Bpred_weights.{epoch:02d}-{val_loss:.2f}.hdf5", monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False, mode='auto', period=1) results = model.fit_generator(generator=training_generator, validation_data=validation_generator, epochs=100, callbacks=[cb_1,cb_2]) model.save_weights("model_1_weights.h5") history_1_pred = results.history pickle.dump( history_1_pred, open( "history_1_pred.pkl", "wb" ) ) params = {'dim': (160,32,160), 'batch_size': 1, 'n_classes': 3, 'n_channels': 4, 'shuffle': False} validation_generator = SingleDataGenerator(train_val_test_dict['test'], **params)
class SiameseNetwork: def __init__(self, shape, modelName, learningRate=1.0): self.learningRate = learningRate self.modelName = modelName left_input = Input(shape) right_input = Input(shape) # Define Siamese Network using shared weights L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1])) L1_distance = L1_layer([left_input, right_input]) hidden = Dense(512, activation='relu')(L1_distance) hidden2 = Dense(64, activation='relu')(hidden) prediction = Dense(1, activation='sigmoid')(hidden2) self.siamese_net = Model(inputs=[left_input, right_input], outputs=prediction) # Compile and prepare network self.siamese_net.compile(loss="binary_crossentropy", optimizer=Adadelta(self.learningRate), metrics=['accuracy']) def trainModel(self, trainDatagen, valGen, epochs, batch_size, verbose=1): early_stop = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=5, verbose=verbose) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.01, verbose=verbose) self.siamese_net.fit_generator(trainDatagen ,steps_per_epoch=320000 / batch_size, epochs=epochs #,validation_data=valGen, validation_steps = 80000 / batch_size ,callbacks=[early_stop, reduce_lr]) def finetune(self, X, Y, epochs, batch_size, verbose=1): early_stop = EarlyStopping(monitor='val_loss', min_delta=0.1, patience=5, verbose=1) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=0.01, verbose=verbose) self.siamese_net.fit(self.preprocess(X), Y, batch_size=batch_size, epochs=epochs, validation_split=0.2, verbose=verbose, callbacks=[early_stop, reduce_lr]) def testAccuracy(self, X, Y, batch_size=512): n_correct, total = 0, 0 X_left, X_right, Y_send = [], [], [] for i, x in enumerate(X): for j, y in enumerate(X): X_left.append(x) X_right.append(y) Y_send.append(1*(Y[i] == Y[j])) if len(X_left) == batch_size: Y_send = np.stack(Y_send) predictions = np.argmax(self.siamese_net.predict([np.stack(X_left), np.stack(X_right)]), axis=1) n_correct += np.sum(predictions == Y_send) total += len(X_left) X_left, X_right, Y_send = [], [], [] if len(X_left) > 0: Y_send = np.stack(Y_send) predictions = np.argmax(self.siamese_net.predict([np.stack(X_left), np.stack(X_right)]), axis=1) n_correct += np.sum(predictions == Y_send) total += len(X_left) return n_correct / float(total) def customTrainModel(self, dataGen, epochs, batch_size, valRatio=0.2, n_steps=320000): steps_per_epoch = int(n_steps / batch_size) for _ in range(epochs): train_loss, val_loss = 0, 0 train_acc, val_acc = 0, 0 for i in range(steps_per_epoch): x, y = next(dataGen) # Split into train and val indices = np.random.permutation(len(y)) splitPoint = int(len(y) * valRatio) x_train, y_train = [ pp[indices[splitPoint:]] for pp in x], y[indices[splitPoint:]] x_test, y_test = [ pp[indices[:splitPoint]] for pp in x], y[indices[:splitPoint]] train_metrics = self.siamese_net.train_on_batch(x_train, y_train) train_loss += train_metrics[0] train_acc += train_metrics[1] val_metrics = self.siamese_net.test_on_batch(x_test, y_test) val_loss += val_metrics[0] val_acc += val_metrics[1] sys.stdout.write("%d / %d : Tr loss: %f, Tr acc: %f, Vl loss: %f, Vl acc: %f \r" % (i+1, steps_per_epoch, train_loss/(i+1), train_acc/(i+1), val_loss/(i+1), val_acc/(i+1))) sys.stdout.flush() print("\n") def maybeLoadFromMemory(self): try: self.siamese_net.load_weights(self.modelName + ".h5") return True except: return False def save(self, customName=None): if not customName: self.siamese_net.save_weights(self.modelName + ".h5") else: self.siamese_net.save_weights(customName + ".h5") def preprocess(self, X): return X def predict(self, X): return self.siamese_net.predict(self.preprocess(X), batch_size=1024)
def train(): """Construct model and train it""" inputs = Input(shape=data.INPUT_SHAPE) # load pre-trained model base_model = ResNet50(input_shape=data.INPUT_SHAPE, weights='imagenet', include_top=False) # freeze pre-trained weights for layer in base_model.layers: layer.trainable = False x = base_model(inputs) # flatten multi-dimensional structure into one dimension x = Flatten()(x) class_output = Dense(2, activation='softmax')(x) # no idea what activation would be good here box_coordinates = Dense(4, activation='relu')(x) model = Model(inputs=inputs, outputs=[class_output, box_coordinates]) # compile the model # (this should be done after setting layers to non-trainable) model.compile(optimizer='rmsprop', loss=['binary_crossentropy', 'mean_squared_error'], loss_weights=[1 / 0.6, 1 / 10000]) # callbacks # saves network architecture and weights after each epoch cp_dir = CHECKPOINT_ROOT_DIR + os.sep + time.strftime("%Y-%m-%d_%H-%M-%S") os.makedirs(cp_dir) filepath = cp_dir + os.sep + \ "model_epoch{epoch:02d}_val_loss{val_loss:.2f}.hdf5" model_checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1) # writes information about training process to the specified folder in # TesorBoard format. TensorBoard can then visualize these logs in a # browser batch_size = 10 tensor_board = callbacks.TensorBoard(log_dir=TENSORBOARD_LOG_PATH, histogram_freq=0, batch_size=batch_size, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None) # `steps_per_epoch` is the number of batches to draw from the generator # at each epoch. model.fit_generator(generator=train_generator(data.train_data, batch_size, data.INPUT_SHAPE), validation_data=validation_generator( data.validation_data, batch_size, data.INPUT_SHAPE), validation_steps=3, steps_per_epoch=500, epochs=10, workers=1, callbacks=[model_checkpoint, tensor_board]) return model
) train_batches = train_generator.flow_from_directory('flowers/train', target_size=(224, 224), batch_size=4) val_generator = ImageDataGenerator(preprocessing_function=preprocess_input) val_batches = val_generator.flow_from_directory('flowers/val', target_size=(224, 224), batch_size=4) indices = train_batches.class_indices labels = [None] * 17 for key in indices: labels[indices[key]] = key pretrained = VGG19(include_top=False, input_shape=(224, 224, 3), weights='imagenet', pooling='max') for layer in pretrained.layers: layer.trainable = False inputs = pretrained.input outputs = pretrained.output print(inputs) print(outputs) hidden = Dense(128, activation='relu')(outputs) dropout = Dropout(.3)(hidden) preds = Dense(17, activation='softmax')(dropout) model = Model(inputs, preds) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['acc']) model.fit_generator(train_batches, epochs=100, validation_data=val_batches)
self.best_acc = [] self.dataset = dataset def on_epoch_end(self, epoch, logs={}): score, acc = self.model.evaluate_generator(get_batch(self.dataset), steps=self.dataset.epoch) self.best_acc.append(acc) print(("best test -los:{} -acc:{}".format(score, acc))) def on_train_end(self, logs={}): print(("test acc list: " + str(self.best_acc))) print(("BestTest acc:{}".format(max(self.best_acc)))) print(model.summary()) # plot_model(model, to_file='../figure/hierarchicalCNN_No_Shape.png', show_shapes=False) # plot_model(sent_model, to_file='../figure/sentModel_No_Shape.png', show_shapes=False) checkpointer = ModelCheckpoint(filepath="../save/weights.hdf5", verbose=1, monitor='val_acc', save_best_only=True, save_weights_only=False) testCallback = TestHistory(dataset=testset) model.fit_generator(get_batch(dataset=trainset), steps_per_epoch=trainset.epoch, epochs=15, validation_data=get_batch(dataset=devset), validation_steps=devset.epoch, callbacks=[testCallback])
def fit(self, sp, X_tr, X_tr_aux, y_tr, X_val, X_val_aux, y_val): keras.backend.clear_session() print(X_tr.shape) input_img = Input(shape=(X_tr.shape[1:])) # We don't use it in this CNN. input_aux = Input(shape=[X_tr_aux.shape[1]]) x = Conv2D(32, kernel_size=(3, 3))(input_img) x = LeakyReLU()(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x) x = Dropout(0.1)(x) x = Conv2D(64, kernel_size=(3, 3))(x) x = LeakyReLU()(x) x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x) x = Dropout(0.4)(x) x = Flatten()(x) x = Dense(128)(x) x = LeakyReLU()(x) x = Dropout(sp['dropout1'])(x) x = Dense(10, activation='softmax')(x) model = Model([input_img, input_aux], x) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=Adam(lr=sp['lr']), metrics=['accuracy']) batch_size = sp['batch_size'] generator = ImageDataGenerator( horizontal_flip=True, vertical_flip=True, rotation_range=20, zoom_range=0.2, shear_range=0.2, ) iter_ = generator.flow( X_tr, range(len(X_tr)), batch_size=batch_size, seed=123, ) def datagen(): while True: X_batch, idx = iter_.next() yield [X_batch, X_tr_aux[idx]], y_tr[idx] best_weights = '.best_weights.h5' model.fit_generator(generator=datagen(), steps_per_epoch=int(X_tr.shape[0] / batch_size), epochs=100, verbose=2, validation_data=([X_val, X_val_aux], y_val), callbacks=[ EarlyStopping(monitor='val_loss', patience=10, verbose=1), ModelCheckpoint(best_weights, save_best_only=True, monitor='val_loss'), ]) model.load_weights(filepath=best_weights) return model
model_dict = get_model_dict_from_db(model_str, opts) input = model_dict['in'] output = model_dict['out'] for i in np.arange(opt_utils.get_epoch(opts) / reset_step): model = Model(model_dict['in'], model_dict['out']) # for layer in model.layers: # k = np.random.binomial(1,.5) # layer.trainable = bool(k) model.summary() model.compile(loss=opt_utils.get_loss(opts), optimizer=optimizer, metrics=opt_utils.get_metrics(opts)) # TRAIN samples_per_epoch = data_train.shape[0] if opts[ 'training_opts']['samples_per_epoch'] == -1 else opts[ 'training_opts']['samples_per_epoch'] model.fit_generator(data_gen.flow( data_train, label_train, batch_size=opts['training_opts']['batch_size'], shuffle=True, seed=opts['seed']), samples_per_epoch=samples_per_epoch, nb_epoch=reset_step, callbacks=callback_list, validation_data=(data_test, label_test)) except: print(model_str, dataset_str) traceback.print_exc()
indices = train_batches.class_indices labels = [None] * 17 for key in indices: labels[indices[key]] = key pretrained = VGG19(include_top=False, input_shape=(224, 224, 3), weights='imagenet', pooling='max') for layer in pretrained.layers: layer.trainable = False inputs = pretrained.input outputs = pretrained.output hidden = Dense(128, activation='relu')(outputs) dropout = Dropout(.3)(hidden) preds = Dense(17, activation='softmax')(dropout) model = Model(inputs, preds) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['acc']) model.fit_generator(train_batches, epochs=100, validation_data=val_batches, steps_per_epoch=len(train_batches), validation_steps=len(val_batches))
# custom_vgg_model = vgg_model # fine-tuning by GAP print(custom_vgg_model.summary()) train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, validation_split=0) train_generator = train_datagen.flow_from_directory( '../../data/train', target_size=(200, 200), batch_size=32, class_mode='categorical', subset="training") validation_generator = train_datagen.flow_from_directory( '../../data/train', target_size=(200, 200), batch_size=32, class_mode='categorical', subset="validation") custom_vgg_model.compile(optimizer=keras.optimizers.Adam(lr=1e-7), loss=classification_loss_stage2, metrics=["accuracy", square_loss_tec, cross_loss_tec]) # custom_vgg_model.compile(optimizer=keras.optimizers.Adam(lr=1e-7), loss=keras.losses.categorical_crossentropy, metrics=["accuracy", square_loss_tec, cross_loss_tec]) model_save_callback = keras.callbacks.ModelCheckpoint("./saved_models/model%s.h5" % time.strftime('%Y-%m-%d-%H-%M-%S'), monitor="val_loss", verbose=1, save_best_only=False, mode="auto") custom_vgg_model.fit_generator(train_generator, steps_per_epoch=5000, epochs=2, validation_data=validation_generator, validation_steps=200, callbacks=[model_save_callback]) # custom_vgg_model.fit(x_train, y_train, batch_size=32, epochs=300, verbose=1, validation_data=(x_test, y_test), shuffle=True, callbacks=[model_save_callback]) # keras.models.save_model(custom_vgg_model, "./saved_models/final_model_" + time.strftime('%Y-%m-%d-%H-%M-%S') + ".h5")
monitor='val_acc', verbose=1, save_best_only=True, mode='max') print(" --nb_train_samples: " + str(nb_train_samples) + "\n" + " --nb_validation_samples: " + str(nb_validation_samples) + "\n" + " --nb_epoch: " + str(nb_epoch) + "\n" + " --nb_classes: " + str(nb_classes) + "\n" + " --learning rate: " + str(learningrate) + "\n" + " --momentum: " + str(momentum) + "\n" + " --batchtrainsize: " + str(batch_trainsize) + "\n" + " --batchvalidationsize: " + str(batch_testsize) + "\n" + " --optimizer: SGD\n" + " --metrics: accuracy\n" + " --model: VGG16 (until block3_pool layer)\n") # fine-tune the model model.fit_generator(train_generator, steps_per_epoch=nb_train_samples, epochs=nb_epoch, validation_data=validation_generator, validation_steps=nb_validation_samples, callbacks=[tb, checkpoint]) # serialize model to JSON model_json = model.to_json() with open("./outputs/model.json", "w") as json_file: json_file.write(model_json) # save the model !!!Be careful, if you want to load it again, delete "_v2" tag in name model.save('./trained_models\cifar10-vgg16_model_alllayers_v2.h5') del model #prevent memory leak
pred = Dense(10, activation='sigmoid')(x) model = Model(base_model.input, pred) for layer in base_model.layers: layer.trainable = False model.compile(loss='binary_crossentropy', optimizer=optimizers.SGD(lr=1e-5, momentum=0.9), metrics=['accuracy']) model.summary() train_datagen = ImageDataGenerator( rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_datagen.fit(X_train) train_generator = train_datagen.flow(X_train, Y_train, batch_size=32) test_datagen = ImageDataGenerator(rescale=1. / 255) validation_generator = test_datagen.flow(X_test, Y_test, batch_size=32) model.fit_generator( train_generator, samples_per_epoch=nb_train_samples, nb_epoch=nb_epoch, validation_data=validation_generator, nb_val_samples=nb_validation_samples)
top_model.add(Flatten(input_shape=base_model.output_shape[1:])) top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(.5)) top_model.add(Dense(1, activation='sigmoid')) model = Model(input=base_model.input, output=top_model(base_model.output)) for layer in model.layers[:19]: layer.trainable = False model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy']) tensorboard = TensorBoard(log_dir=os.path.join(SAVE_PATH, 'logs'), histogram_freq=1, write_graph=True, write_images=False) model.fit_generator(train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=2000, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size, callbacks=[ ModelCheckpoint(filepath=os.path.join( SAVE_PATH, 'checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'), save_best_only=True), tensorboard ])
x = BatchNormalization()(x) x = Dropout(DROPOUT)(x) out = Dense(NUM_CLASSES, activation="softmax")(x) model = Model(inputs=densenet_model.input, outputs=out) ################################################################################################################################################################################################################# # COMPILE, FINE-TUNE/TRAIN AND EVALUATE THE MODEL ################################################################################################################################################################################################################# model.compile(loss=CLASS_MODE + "_crossentropy", optimizer="adam", metrics=["accuracy"]) K.set_value(model.optimizer.lr, LEARNING_RATE) model.fit_generator(train_it, shuffle=True, epochs=NUM_EPOCHS, steps_per_epoch=int(TOT_TRAINING_IMAGES / BATCH_SIZE), validation_data=val_it, validation_steps=int(TOT_VALIDATION_IMAGES / BATCH_SIZE)) scores = model.evaluate_generator(test_it, steps=24) print("Accuracy: %.2f%%" % (scores[1] * 100)) ################################################################################################################################################################ # SAVE THE CURRENT SESSION AND THE FINAL MODEL ################################################################################################################################################################ saver = tf.train.Saver() sess = K.get_session() saver.save(sess, SAVE_DIR + timestamp + "/session.ckpt")
model.compile(optimizer=opt, loss=custom_mse, metrics=[root_mean_squared_error, mean_squared_error]) model.summary() start_from = 0 save_every_n_epoch = 1 n_epochs = 10000 # model.load_weights("../weights/implementation9-bn-24.h5") # start image downloader ip = None g = h5_small_vgg_generator(b_size, "../data/h5_small_train", ip) gval = h5_small_vgg_generator(b_size, "../data/h5_small_validation", None) for i in range(start_from // save_every_n_epoch, n_epochs // save_every_n_epoch): print("START", i * save_every_n_epoch, "/", n_epochs) history = model.fit_generator(g, steps_per_epoch=100000//b_size, epochs=save_every_n_epoch, validation_data=gval, validation_steps=(10000//b_size)) model.save_weights("../weights/implementation9-bn-" + str(i * save_every_n_epoch) + ".h5") # save sample images whole_image_check_overlapping(model, 80, "imp9-bn-" + str(i * save_every_n_epoch) + "-") # save history output = open('../history/imp9-bn-{:0=4d}.pkl'.format(i * save_every_n_epoch), 'wb') pickle.dump(history.history, output) output.close()
def fit(self, sp, X_tr, X_tr_aux, y_tr, X_val, X_val_aux, y_val): keras.backend.clear_session() print(sp) ## Init model input_bands = Input(shape=X_tr.shape[1:]) base_model = ResNet50(input_shape=input_bands.shape[1:], input_tensor=input_bands, include_top=False, classes=1, pooling='avg') x = base_model.output x = Dropout(sp['dropout1'])(x) if sp['batchnorm'] == 1: x = BatchNormalization()(x) x = Dense(sp['n_filter'], activation='relu')(x) x = LeakyReLU()(x) x = Dropout(sp['dropout2'])(x) x = Dense(1, activation='sigmoid', kernel_initializer='he_normal', name='pred')(x) model = Model(input_bands, x) model.compile(loss='binary_crossentropy', optimizer=Adam(lr=sp['lr']), metrics=['accuracy']) batch_size = sp['batch_size'] now = datetime.datetime.now() ## Train ### Warm up (train only classify layer ) for layer in base_model.layers: layer.trainable = False model.compile(loss='binary_crossentropy', optimizer=Adam(lr=sp['lr']), metrics=['accuracy']) generator = ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=25, fill_mode='wrap') iter_ = generator.flow(X_tr, range(len(X_tr)), batch_size=batch_size, seed=123) def datagen(): while True: X_batch, idx = iter_.next() yield X_batch, y_tr[idx] best_weights = 'weights/weights.hdf5' model.fit_generator( generator=datagen(), steps_per_epoch=int(len(X_tr) / batch_size), epochs=10, validation_data=(X_val, y_val), callbacks=[ EarlyStopping(monitor='val_loss', patience=25, verbose=1), ModelCheckpoint(best_weights, save_best_only=True, monitor='val_loss'), keras.callbacks.TensorBoard( log_dir="logs/chipping_{:%Y%m%dT%H%M}".format(now), histogram_freq=0, write_graph=True, write_images=False), ]) ### Train all model for layer in base_model.layers: layer.trainable = True model.compile(loss='binary_crossentropy', optimizer=Adam(lr=sp['lr']), metrics=['accuracy']) model.fit_generator(generator=datagen(), steps_per_epoch=int(len(X_tr) / batch_size), epochs=150, validation_data=(X_val, y_val), callbacks=[ EarlyStopping(monitor='val_loss', patience=25, verbose=1), ModelCheckpoint(best_weights, save_best_only=True, monitor='val_loss'), ]) model.load_weights(filepath=best_weights) return model
def main(): # The data, shuffled and split between train and test sets: (x_train, y_train), (x_test, y_test) = cifar10.load_data() print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') # 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) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 inputs = Input(shape=(32, 32, 3)) out1, out2 = simple_cnn(inputs) model = Model(inputs=inputs, outputs=[out1, out2]) # model.summary() opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) model.compile(optimizer=opt, loss=['categorical_crossentropy', 'categorical_crossentropy'], metrics=['accuracy']) if not data_augmentation: print('Not using data augmentation.') model.fit(x_train, [y_train, y_train], batch_size=batch_size, epochs=epochs, validation_data=(x_test, [y_test, y_test]), shuffle=True) else: print('Using real-time data augmentation.') n_process = 4 pool = multiprocessing.Pool(processes=n_process) def train_generator(x, y, batch_size): train_datagen = T.ImageDataGenerator( pool=pool, 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 generator = train_datagen.flow(x, y, batch_size=batch_size) while 1: x_batch, y_batch = generator.next() yield (x_batch, [y_batch, y_batch]) # Fit the model on the batches generated by datagen.flow(). model.fit_generator(generator=train_generator(x_train, y_train, batch_size), steps_per_epoch=int(y_train.shape[0] / batch_size), epochs=epochs, validation_data=(x_test, [y_test, y_test]), callbacks=[]) pool.terminate() # Save model and weights if not os.path.isdir(save_dir): os.makedirs(save_dir) model_path = os.path.join(save_dir, model_name) model.save(model_path) print('Saved trained model at %s ' % model_path) # Score trained model. scores = model.evaluate(x_test, [y_test, y_test], verbose=1) print('Test loss:', scores[0]) print('Test accuracy:', scores[1])
train_datagen.fit(X_train) train_generator = train_datagen.flow(X_train, Y_train, batch_size=BATCH_SIZE) #%% val_datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=False) val_datagen.fit(X_val) val_generator = val_datagen.flow(X_val, Y_val, batch_size=BATCH_SIZE) #%% md ## Train the Model #%% train_steps_per_epoch = X_train.shape[0] // BATCH_SIZE val_steps_per_epoch = X_val.shape[0] // BATCH_SIZE history = model.fit_generator(train_generator, steps_per_epoch=train_steps_per_epoch, validation_data=val_generator, validation_steps=val_steps_per_epoch, epochs=EPOCHS, verbose=1) #%% md ## Analyze Model Performance #%% f, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5)) t = f.suptitle('Deep Neural Net Performance', fontsize=12) f.subplots_adjust(top=0.85, wspace=0.3) epochs = list(range(1, EPOCHS + 1)) ax1.plot(epochs, history.history['acc'], label='Train Accuracy') ax1.plot(epochs, history.history['val_acc'], label='Validation Accuracy') ax1.set_xticks(epochs) ax1.set_ylabel('Accuracy Value') ax1.set_xlabel('Epoch')
model.compile(optimizer=opt, loss=custom_mse) model.summary() start_from = 160 save_every_n_epoch = 10 n_epochs = 300 model.load_weights("../weights/implementation7l-150.h5") g = image_processing.image_generator_parts(list_dir, b_size, im_size=(224, 224)) for i in range(start_from // save_every_n_epoch, n_epochs // save_every_n_epoch): print("START", i * save_every_n_epoch, "/", n_epochs) history = model.fit_generator(g, steps_per_epoch=len(list_dir) // b_size, epochs=save_every_n_epoch) model.save_weights("../weights/implementation7l-" + str(i * save_every_n_epoch) + ".h5") # save sample images whole_image_check(model, 20, "imp7l-" + str(i) + "-") # save history output = open('../history/imp7l-{:0=4d}.pkl'.format(i), 'wb') pickle.dump(history.history, output) output.close()
class NonConvexEstimator(BaseEstimator): def __init__(self, alpha=1., n_components=25, step_size=1e-3, latent_dropout_rate=0., input_dropout_rate=0., coef_init=None, optimizer='adam', intercept_init=None, batch_size=256, latent_sparsity=None, use_generator=False, random_state=None, n_jobs=1, max_iter=1000): self.alpha = alpha self.n_components = n_components self.max_iter = max_iter self.step_size = step_size self.latent_dropout_rate = latent_dropout_rate self.input_dropout_rate = input_dropout_rate self.coef_init = coef_init self.intercept_init = intercept_init self.batch_size = batch_size self.optimizer = optimizer self.latent_sparsity = latent_sparsity self.random_state = random_state self.n_jobs = n_jobs self.use_generator = use_generator def fit(self, Xs, ys, dataset_weights=None): self.random_state = check_random_state(self.random_state) n_datasets = len(Xs) n_samples = sum(X.shape[0] for X in Xs) n_features = Xs[0].shape[1] # Data sizes = np.array([y.shape[1] for y in ys]) limits = [0] + np.cumsum(sizes).tolist() n_targets = limits[-1] if self.n_components == 'auto': n_components = n_targets else: n_components = self.n_components self.slices_ = [] for iter in range(n_datasets): self.slices_.append(np.array([limits[iter], limits[iter + 1]])) self.slices_ = tuple(self.slices_) if dataset_weights is None: dataset_weights = [1.] * n_datasets dataset_weights /= np.sum(dataset_weights) / n_datasets # dataset_weights = np.array(dataset_weights) * np.sqrt([X.shape[0] for X in Xs]) padded_ys = [] masks = [] Xs = [X.copy() for X in Xs] for y, this_slice in zip(ys, self.slices_): padded_y = np.zeros((y.shape[0], n_targets)) mask = np.zeros((y.shape[0], n_targets), dtype='bool') mask[:, this_slice[0]:this_slice[1]] = 1 padded_y[:, this_slice[0]:this_slice[1]] = y padded_ys.append(padded_y) masks.append(mask) if self.use_generator: our_generator = generator(Xs, padded_ys, masks, batch_size=self.batch_size, dataset_weights=dataset_weights, random_state=self.random_state) if self.batch_size is not None: steps_per_epoch = n_samples / self.batch_size else: steps_per_epoch = n_datasets else: Xs_cat = np.concatenate(Xs, axis=0) padded_ys_cat = np.concatenate(padded_ys, axis=0) masks_cat = np.concatenate(masks, axis=0) sample_weight = np.concatenate( [[dataset_weight * n_samples / X.shape[0] / n_datasets] * X.shape[0] for dataset_weight, X in zip(dataset_weights, Xs)]) if self.batch_size is None: batch_size = n_samples else: batch_size = self.batch_size # Model if self.intercept_init is not None: supervised_bias_initializer = \ lambda shape: K.constant(self.intercept_init) else: supervised_bias_initializer = 'zeros' if self.coef_init is not None: U, S, VT = svd(self.coef_init, full_matrices=False) latent_init = U[:, :n_components] latent_init *= S[:n_components] supervised_init = VT[:n_components] if n_components > latent_init.shape[1]: latent_init = np.concatenate([ latent_init, np.zeros((latent_init.shape[0], n_components - latent_init.shape[1])) ], axis=1) supervised_init = np.concatenate([ supervised_init, np.zeros((n_components - supervised_init.shape[0], supervised_init.shape[1])) ], axis=0) supervised_kernel_initializer = \ lambda shape: K.constant(supervised_init) latent_kernel_initializer = \ lambda shape: K.constant(latent_init) else: supervised_kernel_initializer = 'glorot_uniform' latent_kernel_initializer = 'glorot_uniform' data = Input(shape=(n_features, ), name='data', dtype='float32') mask = Input(shape=(n_targets, ), name='mask', dtype='bool') dropout_data = Dropout(rate=self.input_dropout_rate)(data) if n_components is not None: latent = Dense(n_components, name='latent', use_bias=False, kernel_initializer=latent_kernel_initializer, kernel_regularizer=l2(self.alpha))(dropout_data) latent = Dropout(rate=self.latent_dropout_rate)(latent) else: latent = dropout_data logits = Dense(n_targets, use_bias=True, name='supervised', kernel_initializer=supervised_kernel_initializer, bias_initializer=supervised_bias_initializer, kernel_regularizer=l2(self.alpha))(latent) prediction = PartialSoftmax()([logits, mask]) self.model_ = Model(inputs=[data, mask], outputs=prediction) if self.optimizer == 'adam': optimizer = Adam elif self.optimizer == 'sgd': optimizer = SGD elif self.optimizer == 'rmsprop': optimizer = RMSprop else: raise ValueError('Wrong optimizer') self.model_.compile(optimizer(self.step_size), loss='categorical_crossentropy', metrics=['accuracy']) schedule = lambda i: 1e-3 if i < self.max_iter // 2 else 1e-4 scheduler = LearningRateScheduler(schedule) callbacks = [scheduler] if self.latent_sparsity is not None: callbacks.append(L1ProjCallback(radius=self.latent_sparsity)) sess = tf.Session( config=tf.ConfigProto(device_count={'CPU': self.n_jobs}, inter_op_parallelism_threads=self.n_jobs, intra_op_parallelism_threads=self.n_jobs, use_per_session_threads=True)) K.set_session(sess) if self.use_generator: self.model_.fit_generator(our_generator, steps_per_epoch=steps_per_epoch, verbose=2, epochs=self.max_iter, callbacks=callbacks) else: self.model_.fit([Xs_cat, masks_cat], padded_ys_cat, sample_weight=sample_weight, batch_size=batch_size, verbose=2, epochs=self.max_iter, callbacks=callbacks) supervised, self.intercept_ = self.model_.get_layer( 'supervised').get_weights() if self.n_components is not None: latent = self.model_.get_layer('latent').get_weights()[0] self.coef_ = latent.dot(supervised) else: self.coef_ = supervised def predict(self, Xs): n_datasets = len(Xs) X_cat = np.concatenate(Xs, axis=0) n_targets = self.model_.output_shape[1] sample_sizes = np.array([X.shape[0] for X in Xs]) limits = [0] + np.cumsum(sample_sizes).tolist() sample_slices = [] for i in range(n_datasets): sample_slices.append(np.array([limits[i], limits[i + 1]])) sample_slices = tuple(sample_slices) masks = [] for X, this_slice in zip(Xs, self.slices_): mask = np.zeros((X.shape[0], n_targets), dtype='bool') mask[:, this_slice[0]:this_slice[1]] = 1 masks.append(mask) mask_cat = np.concatenate(masks, axis=0) y_cat = self.model_.predict([X_cat, mask_cat]) ys = [] for X, sample_slice, target_slice in zip(Xs, sample_slices, self.slices_): y = y_cat[sample_slice[0]:sample_slice[1], target_slice[0]:target_slice[1]] ys.append(y) ys = tuple(ys) return ys
if not data_augmentation: print('Not using data augmentation.') history = model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(x_test, y_test), shuffle=True, callbacks=[]) else: print('Using real-time data augmentation.') # realtime data augmentation datagen_train = ImageDataGenerator( featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, rotation_range=0, width_shift_range=0.125, height_shift_range=0.125, horizontal_flip=True, vertical_flip=False) datagen_train.fit(x_train) # fit the model on the batches generated by datagen.flow() history = model.fit_generator(datagen_train.flow(x_train, y_train, batch_size=batch_size, shuffle=True), samples_per_epoch=x_train.shape[0], nb_epoch=nb_epoch, verbose=1, validation_data=(x_test, y_test), callbacks=[])
model = Model(inputs=input_tensor, outputs=predictions) for layer in model.layers: layer.W_regularizer = l2(1e-2) layer.trainable = True model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam( lr=config.lr), metrics=['accuracy']) # print(model.summary()) # ----- TRAINING ----- model.fit_generator(train_generator, steps_per_epoch=config.TRAIN_NUM // 12, validation_data=validate_generator, validation_steps=config.VAL_NUM // 12, callbacks=callbacks, epochs=config.EPOCHS, verbose=1) # ------ TESTING ------ label_map = validate_generator.class_indices print(label_map) # No augmentation on test dataset test_datagen = ImageDataGenerator(rescale=1. / 255) generator = test_datagen.flow_from_directory( config.TEST_DATA, target_size=(config.IMAGE_SIZE, config.IMAGE_SIZE), batch_size=12, class_mode=None, # No labels for test dataset
EMBEDDING_DIMENSION, input_length=TARGET_LENGTH, weights=emb_weights)(target_string) ts = Conv1D(1000, 3, activation='relu')(ts) ts = GlobalMaxPooling1D()(ts) ts = Dropout(0.5)(ts) inp = concatenate([cwp, cws, esp, ess, l2v, ts]) inp = Dense(units=len(ENCODING_MAP_2x2), activation=u'softmax')(inp) model = Model(inputs=[ context_words_pair, context_words_single, entities_strings_pair, entities_strings_single, mapvec, target_string ], outputs=[inp]) model.compile(loss=u'categorical_crossentropy', optimizer=u'rmsprop', metrics=[u'accuracy']) print(u'Finished building model...') # -------------------------------------------------------------------------------------------------------------------- checkpoint = ModelCheckpoint( filepath=u"../data/weights.{epoch:02d}-{acc:.2f}.hdf5", verbose=0) early_stop = EarlyStopping(monitor=u'acc', patience=5) file_name = u"../data/train_wiki_uniform.txt" model.fit_generator( generate_arrays_from_file(file_name, word_to_index), steps_per_epoch=int(check_output(["wc", file_name]).split()[0]) / BATCH_SIZE, epochs=250, callbacks=[checkpoint, early_stop])
#--FC Layer hidden1 = Dense(512,activation='relu',name='hidden1')(last_layer) hidden1 = Dropout(0.5)(hidden1) #--FC Layer hidden2 = Dense(512,activation='relu',name='hidden2')(hidden1) hidden2 = Dropout(0.5)(hidden2) #--Regression Layer out = Dense(1,activation='linear',name='classifier')(hidden2) custom_vgg_model = Model(rgb_model.input,out) custom_vgg_model.compile(loss='mse',optimizer=keras.optimizers.Adam(lr=0.0001,decay=0.0005)) custom_vgg_model.fit_generator(generator(),samples_per_epoch=1000,validation_data=val_generator(),validation_steps=10,epochs=2) #--Here you read the label Y=readCSV(dirLabelsTest+'_Depression.csv') buf=0 numberOfFrames = NUM_FRAMES # while (buf < numberOfFrames): #Insert here the directory of the images of test set imagem = image.load_img(dirTesting,target_size=(FRAME_HEIGHT,FRAME_WIDTH)) imagem = image.img_to_array(imagem) #Subtract the mean of VGGFace2 dataset #---put your function here imga = utils.preprocess_input(imagem,version=2) #here it is the mean value of VGGFace dataset X.append(imga) X = np.array(X) X = np.expand_dims(X,axis=0)