def save_model(model: Sequential, model_name: str, model_num: str): '''Save the current passed in model as model_name. Parameters ---------- model: Sequential Keras model. model_name: str The name of the model. ''' model_json = model.to_json() with open("models/" + model_name + '-' + str(model_num) + ".json", "w") as json_file: json_file.write(model_json) model.save_weights("models/" + model_name + '-' + str(model_num) + ".h5") print("Saved %s to disk" % (model_name + '-' + str(model_num)))
# truncate and pad the input sequences so that they are all in the same length for modeling X = tokenizer.texts_to_sequences(df['Consumer complaint narrative'].values) X = pad_sequences(X, maxlen=MAX_SEQUENCE_LENGTH) print('Shape of data tensor:', X.shape) # Converting categorical labels to numbers Y = pd.get_dummies(df['Product']).values print('Shape of label tensor:', Y.shape) # Train test split X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.10, random_state=42) print(X_train.shape, Y_train.shape) print(X_test.shape, Y_test.shape) model = Sequential() model.add(Embedding(MAX_NB_WORDS, EMBEDDING_DIM, input_length=X.shape[1])) model.add(SpatialDropout1D(0.2)) model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2)) model.add(Dense(13, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) epochs = 5 batch_size = 64 history = model.fit(X_train, Y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)]) # get accuracy score accr = model.evaluate(X_test, Y_test) print('Test set\n Loss: {:0.3f}\n Accuracy: {:0.3f}'.format(accr[0], accr[1]))
def run2(): #Building a Deep Neural Network based classification model model = Sequential() model.add( Dense(164, input_shape=[14], activation='relu', kernel_regularizer='l2', kernel_initializer='TruncatedNormal')) model.add(Dense(164, activation='relu')) model.add(Dense(546 * 2, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(0.5)) model.add(Dense(14, activation='relu', kernel_regularizer='l2')) model.add(BatchNormalization()) model.add(Dense(2, activation='sigmoid')) #Save a copy of the untrained model model.save( '../Custom_Models/Keras_Model_H5/Untrained_Classification_Model.h5') print("Model is saved to '/Untrained_Classification_Model.h5'") import tensorflow model = tensorflow.keras.models.load_model( '../Custom_Models/Keras_Model_H5/Untrained_Classification_Model.h5') return model
def top_model(): top_model = Sequential() top_model.add( Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=conv_base.output_shape[1:])) top_model.add(BatchNormalization()) top_model.add(MaxPool2D(pool_size=(2, 2), strides=(1, 1))) top_model.add(Flatten()) top_model.add(Dense(4096, activation='relu')) top_model.add(BatchNormalization()) top_model.add(Dropout(0.5)) top_model.add(Dense( 14 // ns, activation='relu')) #for ns =2 it will be 14//2 == 7 # Creating a final model based on VGG16 and additional architecture model = Sequential() for layer in conv_base.layers: model.add(layer) model.add(top_model) return model
def fit(image_dir: str = IMAGE_DIR, dump: bool = True, **kwargs): """ Read and resize images Save all the data in: TRAIN_X - pixels TRAIN_Y - labels """ logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = TensorBoard(log_dir=logdir) model_checkpoint = ModelCheckpoint( str(MODELS_DIR / 'weights-improvement-{epoch:02d}-{val_accuracy:.2f}.hdf5'), monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1) reduce_lron = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, mode='auto') base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(WIDHT, HEIGHT, 3)) for layer in base_model.layers: layer.trainable = False model = Sequential() model.add(base_model) model.add(GlobalAveragePooling2D()) model.add(Dense(1024, activation='relu', kernel_regularizer=l2(0.0001))) model.add(Dense(LABEL_SIZE, activation='softmax')) model.compile( loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'], ) train_generator = augs_gen.flow_from_directory( directory=IMAGE_DIR, target_size=WH, batch_size=BATCH_SIZE, seed=1, shuffle=True, subset='training', ) test_generator = augs_gen.flow_from_directory( directory=IMAGE_DIR, target_size=WH, batch_size=BATCH_SIZE, seed=1, shuffle=True, subset='validation', ) labels = (train_generator.class_indices) labels = dict((v, k) for k, v in labels.items()) with open(DATA_DIR / 'generator_labels.dump', 'wb') as file: pickle.dump(labels, file) with graph.as_default(): model.fit_generator( train_generator, validation_data=test_generator, steps_per_epoch=train_generator.samples // BATCH_SIZE, validation_steps=test_generator.samples // BATCH_SIZE, epochs=EPOCHS, verbose=1, callbacks=[tensorboard_callback, model_checkpoint, reduce_lron], ) print('Prepare to write data on the disk') model.save(f'{model_name}.dump')
def build_model(vocab_size, window=3): model = Sequential() model.add(Embedding(vocab_size, EMBEDDING_N_DIM, input_length=window)) model.add(Dropout(0.1)) model.add(LSTM(256, dropout=0.1, recurrent_dropout=0.1)) model.add(Dense(1024, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(vocab_size, activation='softmax')) model.compile( loss='categorical_crossentropy', optimizer='adam', metrics=[ 'accuracy', ], ) return model
def read_dataset(image_dir: str = IMAGE_DIR, dump: bool = True, **kwargs): """ Read and resize images Save all the data in: TRAIN_X - pixels TRAIN_Y - labels """ logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = TensorBoard(log_dir=logdir) base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(WIDHT, HEIGHT, 3)) for layer in base_model.layers: layer.trainable = False model = Sequential() model.add(base_model) model.add(GlobalAveragePooling2D()) model.add(Dense(1024, activation='relu')) model.add(Dense(LABEL_SIZE, activation='softmax')) model.compile( loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'], ) # def define_label(parent_name): # return "-".join(parent_name.split('-')[1:]) # count = 0 # for subdir, dirs, files in os.walk(image_dir): # print(f'PATH: {subdir} is processing') # count += 1 # for file in files: # path = pathlib.Path(subdir).absolute() / file # image = load_img(str(path), target_size=WH) # TRAIN_X.append(np.array(image)) # image_label = define_label(path.parent.name) # TRAIN_Y.append(image_label) # label_encoder = LabelEncoder() # TRAIN_Y = label_encoder.fit_transform(TRAIN_Y) # TRAIN_Y = np.array(to_categorical(TRAIN_Y, num_classes=LABEL_SIZE)) # x_train, x_test, y_train, y_test = train_test_split( # np.array(TRAIN_X), # TRAIN_Y, # test_size=0.2, # random_state=69, # ) train_generator = augs_gen.flow_from_directory( directory=IMAGE_DIR, target_size=WH, batch_size=BATCH_SIZE, seed=1, shuffle=True, subset='training', ) test_generator = augs_gen.flow_from_directory( directory=IMAGE_DIR, target_size=WH, batch_size=BATCH_SIZE, seed=1, shuffle=True, subset='validation', ) labels = (train_generator.class_indices) labels = dict((v, k) for k, v in labels.items()) with open(DATA_DIR / 'generator_labels.dump', 'wb') as file: pickle.dump(labels, file) model.fit_generator( train_generator, validation_data=test_generator, steps_per_epoch=train_generator.samples // BATCH_SIZE, validation_steps=test_generator.samples // BATCH_SIZE, epochs=EPOCHS, verbose=1, callbacks=[tensorboard_callback], ) print('Prepare to write data on the disk') model.save(f'{model_name}_without_iter.dump')
def create_network(): #open up the dataset for reading and training x = pickle.load(open("x.pickle", "rb")) y = pickle.load(open("y.pickle", "rb")) #noramlize the data to a fixed pixel size x = x / 255.0 #start building the model model = Sequential() #create Convolutional layers (filters) model.add(Conv2D(32, (3, 3), input_shape=x.shape[1:])) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) #these layers are hidden model.add(Flatten()) model.add(Dense(128)) model.add(Activation("relu")) model.add(Dense(128)) model.add(Activation("relu")) # the final layer needs to have the same amount of neurons as our categories model.add(Dense(len(categories))) model.add(Activation("softmax")) #copmile the model model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) #training the model with 10 iterations (epochs) history = model.fit(x, y, batch_size=32, epochs=25, validation_split=0.1) #save the model model.save_weights("model.h5") print("Saved model to disk") model.save('CNN.model') return
#преобразуем двумерные изображения в одномерные данные для входа в нейросеть x_train = x_train.reshape( 60000, 784) #60000 изображений, 784 пикселя в каждом - получаем двумерный массив x_test = x_test.reshape(10000, 784) #для тестирования 10000 изображений #нормализуем x_train = x_train / 255 #делим интенсивность каждого пикселя на 255, то есть теперь эти данные в диапозоне от 0 до 1 x_test = x_test / 255 #правильные ответы - номер соответствующего класса от 0 до 10, нейросеть выдает 10 значений по количеству нейронов, которые соответствуют вероятности того, что объект принадлежит данному классу y_train = utils.to_categorical( y_train, 10) #преобразуем метки выхода в категории, пример ниже (10 классов) y_test = utils.to_categorical(y_test, 10) #теперь создаем саму нейросеть(модель) model = Sequential() #добавляем слои(2 слоя, входной на 800 и выходной на 10 нейронов) model.add( Dense(800, input_dim=784, activation="relu") ) #800 - количество нейронов, input_dim - количество входов в каждый нейрон(по количеству пикселей в изображении), activation - функция активации, в данном случае - ReLu - полулинейная функция (при x < 0 , y = 0, при x > 0, y = x) model.add( Dense(400, activation="relu") ) #добавление дополнительных слоев в разы ускорило скорость обучения, но скорее начало наступать и переобучение и поэтому приходится использовать меньше эпох model.add(Dense(200, activation="relu")) model.add( Dense(10, activation="softmax") ) #здесь функция активации softmax - мягкий максимум(нормализованная экспоненциальная функция, она позволяет получить суммарное значение всех нейронов на выходе равным 1, то есть трактовать выход нейронов как вероятность) #компилируем модель model.compile(loss="categorical_crossentropy",
def create_new_model(regression: bool, class_num: int) -> Sequential: '''Create a VGG16 model with 1/2 the layers, return the model. Parameters ---------- regression: bool Is the model a regression model? class_num: int If the model not regression must specify the class num. Returns ------- model: Sequential Keras model. ''' model = Sequential() model.add( Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(112, 112, 3))) model.add(Conv2D(32, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Flatten()) model.add(Dropout(0.2)) model.add(Dense(2048, activation='relu')) model.add(Dense(2048, activation='relu')) # Handle if model is regression if regression: model.add(Dense(1, activation='linear')) else: model.add(Dense(class_num, activation='softmax')) return model
def create_model(**kwargs): base_model = VGG16(include_top=False, input_shape=(WIDHT, HEIGHT, 3), weights='imagenet') for layer in base_model.layers: layer.trainable = False for layer in base_model.layers: print(layer, layer.trainable) model = Sequential() model.add(base_model) model.add(GlobalAveragePooling2D()) model.add(Dropout(0.3)) model.add(Dense(LABEL_SIZE, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
dropout = Dropout(rate=1 / 16)(reduced_input) output_cluster = Dense(units=num_clusters, activation="sigmoid")(dropout) clustering_model = Model(inputs=reduced_input, outputs=output_cluster) clustering_model.compile(optimizer="sgd", loss="categorical_hinge") reduced_data = adata.obsm reduced_data_array = np.zeros(shape=(num_cells, 16)) je = 0 for data_point in reduced_data: reduced_data_array[je] = data_point[0] je += 1 del reduced_data clustering_model.fit(x=reduced_data_array, y=y_clusters, epochs=300) clustering_model.predict(reduced_data_array) from keras.engine.sequential import Sequential jdesc_model = Sequential() first_layer = Dense(units=32, input_shape=(num_genes, ), activation="relu") jdesc_model.add(first_layer) second_layer = Dense(units=16, activation="tanh") jdesc_model.add(second_layer) third_layer = Dense(units=num_clusters, activation="sigmoid") jdesc_model.add(third_layer) jdesc_model.compile(optimizer="sgd", loss="categorical_hinge") jdesc_model_weights = desc_model.get_weights() + clustering_model.get_weights() jdesc_model.set_weights(jdesc_model_weights) jdesc_model_predictions = jdesc_model.predict(adata.X) predicted_clusters_array = jdesc_model_predictions.argmax(axis=1) clusters_array = clusters_ndarray.T[0] error_array = np.zeros(shape=(num_clusters, num_clusters)) correct_array = np.zeros(shape=(num_clusters, ), dtype=int)
def read_dataset(image_dir: str = IMAGE_DIR, dump: bool = True, **kwargs): """ Read and resize images Save all the data in: TRAIN_X - pixels TRAIN_Y - labels """ global TRAIN_X, TRAIN_Y logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = TensorBoard(log_dir=logdir) base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(WIDHT, HEIGHT, 3)) for layer in base_model.layers: layer.trainable = False model = Sequential() model.add(base_model) model.add(GlobalAveragePooling2D()) # model.add(Dense(512, activation='relu')) model.add(Dense(LABEL_SIZE, activation='softmax')) model.compile( loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'], ) def define_label(parent_name): return "-".join(parent_name.split('-')[1:]) for subdir, dirs, files in os.walk(image_dir): for file in files: path = pathlib.Path(subdir).absolute() / file image_label = define_label(path.parent.name) TRAIN_Y.append(image_label) label_encoder = LabelEncoder() TRAIN_Y = label_encoder.fit_transform(TRAIN_Y) TRAIN_Y = np.array(to_categorical(TRAIN_Y, num_classes=LABEL_SIZE)) count = 0 current_length_train_x = 0 for subdir, dirs, files in os.walk(image_dir): print(f'PATH: {subdir} is processing') count += 1 for file in files: path = pathlib.Path(subdir).absolute() / file image = load_img(str(path), target_size=WH) TRAIN_X.append(np.array(image)) if count % 40 == 0: slice_left = current_length_train_x slice_right = slice_left + len(TRAIN_X) current_length_train_x = slice_right # convert to binary matrix (120 labels at all) 2^10 = 128 # normalize image # split image # TODO: make active on resume iterations # if count == 40: # # make empty # TRAIN_X = [] # model = load_model(f'{model_name}_iter_40.dump') # continue x_train, x_test, y_train, y_test = train_test_split( np.array(TRAIN_X), TRAIN_Y[slice_left:slice_right], test_size=0.2, random_state=69, ) # make empty TRAIN_X = [] augs_gen.fit(x_train) model.fit_generator( augs_gen.flow(x_train, y_train, batch_size=25), validation_data=(x_test, y_test), validation_steps=1000, steps_per_epoch=1000, epochs=20, verbose=1, callbacks=[tensorboard_callback], ) del x_train, x_test, y_train, y_test model.save(f'{model_name}_iter_{count}.dump') print(f'Executed {count} / 121') print('Prepare to write data on the disk')
#! /usr/bin/python from keras import backend as k from keras.engine.sequential import Sequential from keras.layers.embeddings import Embedding import numpy as np model = Sequential() model.add(Embedding(100, 64, input_length=10)) input_array = np.random.randint(1000, size=(32,10)) model.compile('rmsprop', 'mse') output_array = model.predict(input_array) assert output_array == (32, 10, 64)