) train_generator = train_gen.flow(train_images, train_labels, batch_size=kwargs['batch_size']) test_gen = ImageDataGenerator( rescale=1 / 255. ) test_generator = test_gen.flow(test_images, test_labels, batch_size=kwargs['batch_size']) optimizer = keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) model.fit(train_generator, validation_data=test_generator, epochs=kwargs['epochs'], steps_per_epoch=kwargs['batch_size']) if __name__ == '__main__': model = vgg.VGG16(Input((28, 28, 1)), 10) train_images = mnist.load_train_images() train_labels = mnist.load_train_labels() test_images = mnist.load_test_images() test_labels = mnist.load_test_labels() train_images = np.expand_dims(train_images, axis=3) train_labels = tf.one_hot(train_labels, 10) test_images = np.expand_dims(test_images, axis=3) test_labels = tf.one_hot(test_labels, 10) train_data = (train_images, train_labels, test_images, test_labels) # train(model, train_data, epochs=EPOCHS, batch_size=BATCH_SIZE)
from tensorflow.keras.layers import Dense from tensorflow.keras import Input, Model from tcn import TCN, tcn_full_summary import numpy as np import pandas as pd import sys batch_size, timesteps, input_dim = None, 50, 1 i = Input(batch_shape=(batch_size, timesteps, input_dim)) o = TCN(return_sequences=True)(i) o = Dense(1)(o) m = Model(inputs=[i], outputs=[o]) m.compile(optimizer='adam', loss='mse', metrics=['acc']) file_name = sys.argv[1] df = pd.read_csv(file_name, index_col="Product_Code") df = df.iloc[:, 54:] #using normalized values for prediction xtrain = df.iloc[:, :50].values #till 51 week data for training ytrain = df.iloc[:, 50].values xtest = df.iloc[:, 1:51].values #52nd week data for testing ytest = df.iloc[:, 51].values xtrain = xtrain[..., np.newaxis] #increasing last dimension ytrain = ytrain[..., np.newaxis]
import cv2 from tensorflow.keras import Input, Model from darknet import darknet_base from predict import predict import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" inputs = Input(shape=(None, None, 3)) outputs, config = darknet_base(inputs) model = Model(inputs, outputs) vidcap = cv2.VideoCapture('data/demo.mp4') success, image = vidcap.read() size = (int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = cv2.VideoWriter_fourcc(*'DIVX') out = cv2.VideoWriter('output.avi', fourcc, 20.0, size) while success: output_image = predict(model, image, config) out.write(output_image) success, image = vidcap.read() vidcap.release() out.release()
def initialize_layers(self): inputs = Input(self.x_shape) _ = self.call(inputs)
def build_model(self): input_shape = (self.seq_length,) inputs = Input(shape=input_shape, dtype='int32') embedding = layers.Embedding( len(self.index_to_vector_map), self.embedding_dimensions, input_length=self.seq_length, trainable=self.embedding_training ) embedding.build((None, len(self.index_to_vector_map))) embedding.set_weights([self.index_to_vector_map]) concatenate = layers.Concatenate(axis=-1) flatten = layers.Flatten(data_format='channels_last') dropout = layers.Dropout(self.dropout_rate) dense = layers.Dense(self.num_classes, activation='softmax') pooled_outputs = [] x_embedding = embedding(inputs) x_embedding = tf.expand_dims(x_embedding, -1) for filter_size in self.filter_sizes: filter_shape = (filter_size, self.embedding_dimensions) conv2d = layers.Conv2D( self.num_filters_per_size, filter_shape, 1, padding='VALID', activation=self.activation_fn, kernel_initializer=initializers.RandomUniform(minval=-0.01, maxval=0.01), bias_initializer=initializers.zeros(), use_bias=True, data_format='channels_last', input_shape=(self.seq_length, self.embedding_dimensions, 1) ) x = conv2d(x_embedding) x = tf.nn.max_pool( x, ksize=[1, self.seq_length - filter_size + 1, 1, 1], strides=[1, 1, 1, 1], padding='VALID' ) x = flatten(x) pooled_outputs.append(x) if len(pooled_outputs) == 1: pooled_outputs = tf.convert_to_tensor(pooled_outputs[0]) else: pooled_outputs = concatenate(pooled_outputs) pooled_outputs = dropout(pooled_outputs) pooled_outputs = dense(pooled_outputs) model = Model(inputs=inputs, outputs=pooled_outputs, name='sentence_cnn') model.compile( loss='categorical_crossentropy', optimizer=optimizers.Adadelta( learning_rate=self.learning_rate, rho=self.lr_decay, epsilon=1e-6, ), metrics=['accuracy'] ) return model
def initialize_layers(self): x = Input((self.res, self.res, self.num_channels)) w = Input((self.num_latent, )) _ = self.call((x, w))
def initialize_layers(self): x = Input(self.x_shape) w = Input((2, self.num_latent)) noise = Input((self.res, self.res, 2)) _ = self.call((x, w, noise))
for i in x1: for j in x2: k = np.cos(i) + np.cos(j) s_d.append(i) s_v.append(j) s_f.append(k) s_d = np.array(s_d) s_v = np.array(s_v) s_f = np.array(s_f) # find approximate function tile_d = tile_coding.Tile1D(number=50) tile_v = tile_coding.Tile1D(number=50) tile_dv = tile_coding.Tile2D(tile_d, tile_v) my_input = Input(shape=[tile_d.number, tile_v.number]) d = layers.Flatten()(my_input) d = layers.Dense(200)(d) d = layers.Dense(100)(d) o = layers.Dense(1)(d) my_model = Model(my_input, o) my_model.compile(optimizer=tf.optimizers.SGD(), loss=tf.losses.mean_squared_error) while True: f = tile_dv.x(s_d, s_v) my_model.fit(f, s_f, batch_size=32, epochs=5) z = my_model.predict(f) if 'fig' not in locals().keys():
def nonsilence_image2lsf_model2(): input_lip = Input(shape=(64, 64, 1)) # encoding lip_conv1 = Conv2D(filters=16, kernel_size=(5, 5), activation='relu', padding='same')(input_lip) lip_conv2 = Conv2D(filters=16, kernel_size=(5, 5), activation='relu', padding='same')(lip_conv1) lip_pooling1 = MaxPooling2D(pool_size=(2, 2))(lip_conv2) lip_conv3 = Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same')(lip_pooling1) lip_conv4 = Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same')(lip_conv3) lip_pooling2 = MaxPooling2D(pool_size=(2, 2))(lip_conv4) lip_conv5 = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(lip_pooling2) lip_conv6 = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(lip_conv5) lip_pooling3 = MaxPooling2D(pool_size=(2, 2))(lip_conv6) lip_conv7 = Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(lip_pooling3) lip_conv8 = Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(lip_conv7) lip_pooling4 = MaxPooling2D(pool_size=(2, 2))(lip_conv8) input_tongue = Input(shape=(64, 64, 1)) # encoding tongue_conv1 = Conv2D(filters=16, kernel_size=(5, 5), activation='relu', padding='same')(input_tongue) tongue_conv2 = Conv2D(filters=16, kernel_size=(5, 5), activation='relu', padding='same')(tongue_conv1) tongue_pooling1 = MaxPooling2D(pool_size=(2, 2))(tongue_conv2) tongue_conv3 = Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same')(tongue_pooling1) tongue_conv4 = Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding='same')(tongue_conv3) tongue_pooling2 = MaxPooling2D(pool_size=(2, 2))(tongue_conv4) tongue_conv5 = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(tongue_pooling2) tongue_conv6 = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(tongue_conv5) tongue_pooling3 = MaxPooling2D(pool_size=(2, 2))(tongue_conv6) flat_lip = Flatten()(lip_pooling4) flat_tongue = Flatten()(tongue_pooling3) cc = concatenate([flat_lip, flat_tongue]) lsf_fc1 = Dense(256, activation='relu', name='lsf_fc1')(cc) lsf_dr1 = Dropout(0)(lsf_fc1) lsf_fc2 = Dense(13, activation='linear', name='lsf_fc2')(lsf_dr1) new_model = Model([input_lip, input_tongue], lsf_fc2, name='transfer_autoencoder_lsf') new_model.summary() return new_model
def __init__(self, latent_dim, input_dim, measures, measure_len, dropout=0.0, maxnorm=None, vae_b1=0.02, vae_b2=0.1): ''' Initiates a new instance of our VAE model for analysing and generating the first meoldic voice of NESM soundtracks. Parameters ---------- latent_dim : int Dimensionality of our compressed latent space. input_dim : list of ints Dimensionality or number of unique values for input. Basically the number of unique notes in first melodic voice. measures : int Outermost dimension of each sample. # of measures in each sample track. measure_len : int Innermost dimension of each sample. Length of each measure in our sample tracks. dropout : float, optional Dropout rate for each layer. This percent of layer output activations get ignored by next layer. Helps imporove fully trained performance and reduce overfitting. The default is 0.0. maxnorm : float, optional Used for putting maxnorm regularization weight contraint on our layer kernels. This limits the size of weights in our model to reduce overfitting. Shown to be especially effective in combination with dropout. The default is None. vae_b1 : float, optional Standard deviation for vae_sampling. The default is 0.02. vae_b2 : float, optional Weight applied to VAE contribution in loss. The default is 0.1. Returns ------- None. ''' # Call to Super #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(VAE, self).__init__() # Save Model Parameters #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ self.latent_dim = latent_dim self.input_dim = input_dim self.measures = measures self.measure_len = measure_len self.dropout = dropout self.vae_b1 = vae_b1 self.vae_b2 = vae_b2 self.maxnrom = maxnorm # Define Encoder #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if maxnorm: kernel_constraint = tf.keras.constraints.MaxNorm(max_value=4) else: kernel_constraint = None x_in = Input(shape=(measures, measure_len, input_dim)) x = Reshape((measures, measure_len * input_dim))(x_in) x = TimeDistributed( Dense(2000, activation='relu', kernel_constraint=kernel_constraint))(x) x = TimeDistributed( Dense(200, activation='relu', kernel_constraint=kernel_constraint))(x) x = Flatten()(x) x = Dense(1600, activation='relu', kernel_constraint=kernel_constraint)(x) z_mean = Dense(latent_dim)(x) z_log_sigma_sq = Dense(latent_dim)(x) self.encoder = tf.keras.Model(inputs=x_in, outputs=[z_mean, z_log_sigma_sq]) # Define Reparameterization #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ z_mean = Input(shape=(latent_dim, )) z_log_sigma_sq = Input(shape=(latent_dim, )) z = Lambda(self.vae_sampling)([z_mean, z_log_sigma_sq]) self.reparameterize = tf.keras.Model(inputs=[z_mean, z_log_sigma_sq], outputs=z) # Define Decoder #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ z = Input(shape=(latent_dim, )) x = Dense(1600, kernel_constraint=kernel_constraint)(z) x = BatchNormalization()(x) x = Activation('relu')(x) if dropout > 0: x = Dropout(dropout)(x) x = Reshape((measures, 200))(x) x = TimeDistributed(Dense(200, kernel_constraint=kernel_constraint))(x) x = TimeDistributed(BatchNormalization())(x) x = Activation('relu')(x) if dropout > 0: x = Dropout(dropout)(x) x = TimeDistributed(Dense(2000, kernel_constraint=kernel_constraint))(x) x = TimeDistributed(BatchNormalization())(x) x = Activation('relu')(x) if dropout > 0: x = Dropout(dropout)(x) x = TimeDistributed( Dense(measure_len * input_dim, activation='sigmoid'))(x) y = Reshape((measures, measure_len, input_dim))(x) self.decoder = tf.keras.Model(inputs=z, outputs=y)
from math import log import numpy as np import tensorflow as tf from config import LEARNING_RATE from tensorflow.keras import Input from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense from tensorflow.keras.regularizers import l2 from tensorflow.keras.layers import LSTM, Embedding from preprocessing import clean_text from tokenizers import Tokenizer from config import VOCAB_SIZE, MAXLEN # +++++++++++++++++++++++++++++++++ seq2seq model to refere layers with their names ++++++++++++++++++++++++++++++++ encoder_inputs = Input(shape=(25, )) encoder_embedding = Embedding(VOCAB_SIZE, 100, input_length=MAXLEN) decoder_embedding = Embedding(VOCAB_SIZE, 100, input_length=MAXLEN) encoder_embeddings = encoder_embedding(encoder_inputs) encoder_lstm = LSTM(256, return_state=True, kernel_regularizer=l2(0.0000001), activity_regularizer=l2(0.0000001)) LSTM_outputs, state_h, state_c = encoder_lstm(encoder_embeddings) encoder_states = [state_h, state_c] decoder_inputs = Input(shape=(25, ), name='decoder_inputs') decoder_lstm = LSTM(256, return_sequences=True, return_state=True,
def build_compembed(): embed = Input(shape=(1024, )) x = Dense(128)(embed) x = ReLU()(x) compembed = Model(inputs=[embed], outputs=[x]) return compembed
def build_ca(): embed = Input(shape=(1024, )) x = Dense(256)(embed) mulogsigma = LeakyReLU(alpha=0.2)(x) ca = Model(inputs=[embed], outputs=[mulogsigma]) return ca
def build_model(nx: Optional[int] = None, ny: Optional[int] = None, channels: int = 1, num_classes: int = 2, layer_depth: int = 5, filters_root: int = 64, kernel_size: int = 3, pool_size: int = 2, dropout_rate: int = 0.5, padding: str = "valid", activation: Union[str, Callable] = "relu", normalization: bool = True) -> Model: """ Constructs a U-Net model :param nx: (Optional) image size on x-axis :param ny: (Optional) image size on y-axis :param channels: number of channels of the input tensors :param num_classes: number of classes :param layer_depth: total depth of unet :param filters_root: number of filters in top unet layer :param kernel_size: size of convolutional layers :param pool_size: size of maxplool layers :param dropout_rate: rate of dropout :param padding: padding to be used in convolutions :param activation: activation to be used :return: A TF Keras model """ inputs = Input(shape=(nx, ny, channels), name="inputs") x = inputs contracting_layers = {} conv_params = dict(filters_root=filters_root, kernel_size=kernel_size, dropout_rate=dropout_rate, padding=padding, activation=activation, normalization=normalization) for layer_idx in range(0, layer_depth - 1): x = ConvBlock(layer_idx, **conv_params)(x) contracting_layers[layer_idx] = x x = layers.MaxPooling2D((pool_size, pool_size))(x) x = ConvBlock(layer_idx + 1, **conv_params)(x) for layer_idx in range(layer_idx, -1, -1): x = UpconvBlock(layer_idx, filters_root, kernel_size, pool_size, padding, activation, normalization)(x) x = CropConcatBlock()(x, contracting_layers[layer_idx]) x = ConvBlock(layer_idx, **conv_params)(x) x = layers.Conv2D(filters=num_classes, kernel_size=(1, 1), kernel_initializer=_get_kernel_initializer( filters_root, kernel_size), strides=1, padding=padding)(x) x = layers.Activation(activation)(x) outputs = layers.Activation("softmax", name="outputs")(x) model = Model(inputs, outputs, name="unet") return model
def run_regressors(train_X, train_y, valid_X, valid_y, test_X, test_y, logger=None, config=None): assert config is not None hyper_params.update(config['paramsGrid']) assert logger is not None rr = logger def define_model(data, architecture, num_labels=1, activation='relu', dropouts=[]): assert '-' in architecture archs = architecture.strip().split('-') net = data pen_layer = net prev_layer = net prev_num_outputs = None prev_block_num_outputs = None prev_stub_output = net for i in range(len(archs)): arch = archs[i] if 'x' in arch: arch = arch.split('x') num_outputs = int(re.findall(r'\d+',arch[0])[0]) layers = int(re.findall(r'\d+',arch[1])[0]) j = 0 aux_layers = re.findall(r'[A-Z]',arch[0]) for l in range(layers): if aux_layers and aux_layers[0] == 'B': if len(aux_layers)>1 and aux_layers[1]=='A': rr.fprint('adding fully connected layers with %d outputs followed by batch_norm and act' % num_outputs) net = Dense(num_outputs, name='fc' + str(i) + '_' + str(j), activation=None)(net) net = BatchNormalization(center=True, scale=True, name='fc_bn'+str(i)+'_'+str(j))(net) if activation =='relu': net = Activation('relu')(net) else: rr.fprint('adding fully connected layers with %d outputs followed by batch_norm' % num_outputs) net = Dense(num_outputs, name='fc' + str(i) + '_' + str(j), activation=activation)(net) net = BatchNormalization(center=True, scale=True, name='fc_bn' + str(i) + '_' + str(j))(net) else: rr.fprint('adding fully connected layers with %d outputs' % num_outputs) net = Dense(num_outputs, name='fc' + str(i) + '_' + str(j), activation=activation)(net) if 'R' in aux_layers: if prev_num_outputs and prev_num_outputs==num_outputs: rr.fprint('adding residual, both sizes are same') net = net+prev_layer else: rr.fprint('adding residual with fc as the size are different') net = net + Dense(num_outputs, name='fc' + str(i) + '_' +'dim_'+ str(j), activation=None)(prev_layer) prev_num_outputs = num_outputs j += 1 prev_layer = net aux_layers_sub = re.findall(r'[A-Z]', arch[1]) if 'R' in aux_layers_sub: if prev_block_num_outputs and prev_block_num_outputs == num_outputs: rr.fprint('adding residual to stub, both sizes are same') net = net + prev_stub_output else: rr.fprint('adding residual to stub with fc as the size are different') net = net + Dense(num_outputs, name='fc' + str(i) + '_' + 'stub_dim_' + str(j), activation=None)(prev_stub_output) if 'D' in aux_layers_sub and (num_labels == 1) and len(dropouts) > i: rr.fprint('adding dropout', dropouts[i]) net = Dropout(1.-dropouts[i], seed=SEED)(net, training=False) prev_stub_output = net prev_block_num_outputs = num_outputs prev_layer = net else: if 'R' in arch: act_fun = 'relu' rr.fprint('using ReLU at last layer') elif 'T' in arch: act_fun = 'tanh' rr.fprint('using TanH at last layer') else: act_fun = None pen_layer = net rr.fprint('adding final layer with ' + str(num_labels) + ' output') net = Dense(num_labels, name='fc' + str(i), activation=act_fun)(net) return net def error_rate(predictions, labels, step=0, dataset_partition=''): return np.mean(np.absolute(predictions - labels)) def error_rate_classification(predictions, labels, step=0, dataset_partition=''): return 100.0 - (100.0 * np.sum(np.argmax(predictions, 1) == labels) / predictions.shape[0]) train_X = train_X.reshape(train_X.shape[0], -1).astype("float32") valid_X = valid_X.reshape(valid_X.shape[0], -1).astype("float32") test_X = test_X.reshape(test_X.shape[0], -1).astype("float32") num_input = train_X.shape[1] batch_size = hyper_params['batch_size'] learning_rate = hyper_params['learning_rate'] optimizer = hyper_params['optimizer'] architecture = config['architecture'] num_epochs = hyper_params['num_epochs'] model_path = config['model_path'] patience = hyper_params['patience'] save_path = config['save_path'] loss_type = config['loss_type'] keras_path = config['keras_path'] last_layer_with_weight = config['last_layer_with_weight'] if 'dropouts' in hyper_params: dropouts = hyper_params['dropouts'] else: dropouts = [] test_metric = mean_squared_error if config['test_metric']=='mae': test_metric = mean_absolute_error if config['test_metric']=='accuracy': test_metric = accuracy_score use_valid = config['use_valid'] EVAL_FREQUENCY = hyper_params['EVAL_FREQUENCY'] train_y = train_y.reshape(train_y.shape[0]).astype("float32") valid_y = valid_y.reshape(valid_y.shape[0]).astype("float32") test_y = test_y.reshape(test_y.shape[0]).astype("float32") train_data = train_X train_labels = train_y test_data = test_X test_labels = test_y validation_data = valid_X validation_labels = valid_y rr.fprint("train matrix shape of train_X: ",train_X.shape, ' train_y: ', train_y.shape) rr.fprint("valid matrix shape of train_X: ",valid_X.shape, ' valid_y: ', valid_y.shape) rr.fprint("test matrix shape of valid_X: ",test_X.shape, ' test_y: ', test_y.shape) rr.fprint('architecture is: ',architecture) rr.fprint('learning rate is ',learning_rate) rr.fprint('model path is ', model_path) model = None inputs = Input(shape=(num_input,), name='elemental_fractions') outputs = define_model(inputs, architecture, dropouts=dropouts) model = Model(inputs=inputs, outputs=outputs, name= 'ElemNet') model.summary(print_fn=lambda x: rr.fprint(x)) if model_path: rr.fprint('Restoring model from %s' % model_path) model_h5 = "%s.h5" % model_path model.load_weights(model_h5) if not last_layer_with_weight: rr.fprint('removing last layer to add model and adding dense layer without weight') newl16 = Dense(1, activation=None)(model.layers[-2].output) model = Model(inputs=model.input, outputs=[newl16]) assert optimizer == 'Adam' if loss_type=='mae': model.compile(loss=tf.keras.losses.mean_absolute_error, optimizer=optimizers.Adam(learning_rate=learning_rate), metrics=['mean_absolute_error']) elif loss_type=='binary': model.compile(loss=tf.keras.losses.binary_crossentropy, optimizer=optimizers.Adam(learning_rate=learning_rate), metrics=[tf.keras.metrics.BinaryAccuracy()]) class LossHistory(Callback): def on_epoch_end(self, epoch, logs={}): #rr.fprint( # 'Step %d (epoch %.2d), %.1f s minibatch loss: %.5f, validation error: %.5f, test error: %.5f, best validation error: %.5f' % ( # step, int(step * batch_size) / train_size, # elapsed_time, l_, val_error, test_error, best_val_error)) rr.fprint('{}: Current epoch: {}, loss: {}, validation loss: {}'.format(datetime.datetime.now(), epoch, logs['loss'], logs['val_loss'])) rr.fprint('start training') early_stopping = EarlyStopping(patience=patience, restore_best_weights=True, monitor='val_loss') checkpointer = ModelCheckpoint(filepath=save_path, verbose=0, save_best_only=True, save_freq='epoch', save_format='tf', period=10) history = model.fit(train_X, train_y, verbose=2, batch_size=batch_size, epochs=num_epochs, validation_data=(valid_X, valid_y), callbacks=[early_stopping, LossHistory(), checkpointer]) if use_valid: test_result = model.evaluate(test_X, test_y, batch_size=32) rr.fprint('the test error is ',test_result) rr.fprint(history.history) model.save(save_path, save_format='tf') filename_json = "%s.json" % keras_path filename_h5 = "%s.h5" % keras_path model_json = model.to_json() with open(filename_json, "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 model.save_weights(filename_h5) rr.fprint('saved model to '+save_path) return
def tcn2(input_shape_x, input_shape_author, input_shape_topic, logger, dense_layers=32, learn_rate=0.001, init="he_normal", num_filters=32, filter_sizes=[3], activation="relu", padding="same", l2_float=0.001, dropout=0.2, skips=True, num_stacks=2): """Convolution Network WITH merging of other feature vectors """ optimizer = Adam(learning_rate=learn_rate) param_dict = { "num filters": num_filters, "filter sizes": filter_sizes, "padding": padding, "activation": activation, "n dense layer": dense_layers, "l2 regu": l2_float, "drop-out": dropout, "kernel init": init, "skip connections": skips, "n stacks": num_stacks, "learn rate": learn_rate } # Check if the right shape was passed as argument if len(input_shape_x) != 2: raise Exception("Input shape needs to have 2 dimensions.", "input shape:", input_shape_x) logger.info( f"input shape post (word/sent, word_emb_dims): {input_shape_x}") # Input layer i_x = Input(shape=input_shape_x) # (words/sent, word_emb) # embedding_size = input_shape[1] # Calculate number of blocks: def calc_dilations(filter_size, field, stacks): import math max_dil = field / filter_size / stacks max_dil = math.ceil(math.log(max_dil) / math.log(2)) dil_list = [2**i for i in range(0, max_dil + 1)] return (dil_list) # Convolutional layes logger.info(f"Filter sizes: {filter_sizes}") tcn_compl = [] for filter_size in filter_sizes: # Determine dilation list: dilation_list = calc_dilations(filter_size=filter_size, field=input_shape_x[0], stacks=num_stacks) logger.info(f"Dilation list: {dilation_list}") o_tcn = TCN(nb_filters=num_filters, kernel_size=filter_size, dilations=dilation_list, padding=padding, use_skip_connections=skips, dropout_rate=dropout, activation=activation, kernel_initializer=init, use_batch_norm=False, use_layer_norm=True, return_sequences=False, nb_stacks=num_stacks)(i_x) tcn_compl.append(o_tcn) # Concatenate and flatten different convolutional # Filter outputs if len(filter_sizes) > 1: tcn_compl = Concatenate()(tcn_compl) o_x = Dropout(dropout)(tcn_compl) else: o_x = Dropout(dropout)(tcn_compl[0]) # o_x = Flatten()(o_x) model_x = Model(inputs=i_x, outputs=o_x) # input layer for author embeddings: i_author = Input(shape=input_shape_author) logger.info( f"input shape author embs: (author_emb_dims): {input_shape_author}") # Input layer for topic embeddings: i_topic = Input(shape=input_shape_topic) logger.info( f"input shape topic embs: (topic_emb_dims): {input_shape_topic}") # Combine these inputs o_comb = Concatenate()([i_author, i_topic]) # Create small submodel model_other = Model(inputs=[i_author, i_topic], outputs=o_comb) # Combine feature vectors features_comb = Concatenate()([model_x.output, model_other.output]) # Dense completely connected layer dense = Dense(units=dense_layers, activation=activation, kernel_initializer=init, kernel_regularizer=l2(l2_float))(features_comb) dense = Dropout(dropout)(dense) # Output layer o = Dense(units=1, activation="sigmoid")(dense) # Define model model_tot = Model(inputs=[model_x.input, model_other.input], outputs=[o]) # Compile model model_tot.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) return (model_tot, param_dict)
def build(self): get_custom_objects().update({'mish': Mish(mish)}) input_sig = Input(shape=self.input_shape) x = self._make_stem(input_sig, stem_width=self.stem_width, deep_stem=self.deep_stem) if self.preact is False: x = BatchNormalization(axis=self.channel_axis, epsilon=1.001e-5)(x) x = Activation(self.active)(x) if self.verbose: print("stem_out", x.shape) x = MaxPool3D(pool_size=(3, 3, 3), strides=(2, 2, 2), padding="same", data_format="channels_last")(x) if self.verbose: print("MaxPool3D out", x.shape) if self.preact is True: x = BatchNormalization(axis=self.channel_axis, epsilon=1.001e-5)(x) x = Activation(self.active)(x) if self.using_cb: second_x = x second_x = self._make_layer(x, blocks=self.blocks_set[0], filters=64, stride=1, is_first=False) second_x_tmp = self._make_Composite_layer(second_x, filters=x.shape[-1], upsample=False) if self.verbose: print('layer 0 db_com', second_x_tmp.shape) x = Add()([second_x_tmp, x]) x = self._make_layer(x, blocks=self.blocks_set[0], filters=64, stride=1, is_first=False) if self.verbose: print("-" * 5, "layer 0 out", x.shape, "-" * 5) b1_b3_filters = [64, 128, 256, 512] for i in range(3): idx = i + 1 if self.using_cb: second_x = self._make_layer(x, blocks=self.blocks_set[idx], filters=b1_b3_filters[idx], stride=2) second_x_tmp = self._make_Composite_layer(second_x, filters=x.shape[-1]) if self.verbose: print('layer {} db_com out {}'.format( idx, second_x_tmp.shape)) x = Add()([second_x_tmp, x]) x = self._make_layer(x, blocks=self.blocks_set[idx], filters=b1_b3_filters[idx], stride=2) if self.verbose: print('----- layer {} out {} -----'.format(idx, x.shape)) x = GlobalAveragePooling3D(name='avg_pool')(x) if self.verbose: print("pool_out:", x.shape) if self.dropout_rate > 0: x = Dropout(self.dropout_rate, noise_shape=None)(x) fc_out = Dense(self.n_classes, kernel_initializer="he_normal", use_bias=False, name="fc_NObias")(x) if self.verbose: print("fc_out:", fc_out.shape) if self.fc_activation: fc_out = Activation(self.fc_activation)(fc_out) model = models.Model(inputs=input_sig, outputs=fc_out) if self.verbose: print("Resnest builded with input {}, output{}".format( input_sig.shape, fc_out.shape)) if self.verbose: print("-------------------------------------------") if self.verbose: print("") return model
def test(lowlight_test_images_path): os.environ['CUDA_VISIBLE_DEVICES'] = '2' input_img = Input(shape=(512, 512, 3)) conv1 = Conv2D(32, (3, 3), strides=(1, 1), activation='relu', padding='same')(input_img) conv2 = Conv2D(32, (3, 3), strides=(1, 1), activation='relu', padding='same')(conv1) conv3 = Conv2D(32, (3, 3), strides=(1, 1), activation='relu', padding='same')(conv2) conv4 = Conv2D(32, (3, 3), strides=(1, 1), activation='relu', padding='same')(conv3) int_con1 = Concatenate(axis=-1)([conv4, conv3]) conv5 = Conv2D(32, (3, 3), strides=(1, 1), activation='relu', padding='same')(int_con1) int_con2 = Concatenate(axis=-1)([conv5, conv2]) conv6 = Conv2D(32, (3, 3), strides=(1, 1), activation='relu', padding='same')(int_con2) int_con3 = Concatenate(axis=-1)([conv6, conv1]) x_r = Conv2D(24, (3, 3), strides=(1, 1), activation='tanh', padding='same')(int_con3) model = Model(inputs=input_img, outputs=x_r) model.load_weights("weights/best.h5") ### load image ### for test_file in glob.glob(lowlight_test_images_path + "*.bmp"): data_lowlight_path = test_file original_img = Image.open(data_lowlight_path) original_size = (np.array(original_img).shape[1], np.array(original_img).shape[0]) original_img = original_img.resize((512, 512), Image.ANTIALIAS) original_img = (np.asarray(original_img) / 255.0) img_lowlight = Image.open(data_lowlight_path) img_lowlight = img_lowlight.resize((512, 512), Image.ANTIALIAS) img_lowlight = (np.asarray(img_lowlight) / 255.0) img_lowlight = np.expand_dims(img_lowlight, 0) # img_lowlight = K.constant(img_lowlight) ### process image ### A = model.predict(img_lowlight) r1, r2, r3, r4, r5, r6, r7, r8 = A[:, :, :, : 3], A[:, :, :, 3: 6], A[:, :, :, 6: 9], A[:, :, :, 9: 12], A[:, :, :, 12: 15], A[:, :, :, 15: 18], A[:, :, :, 18: 21], A[:, :, :, 21: 24] x = original_img + r1 * (K.pow(original_img, 2) - original_img) x = x + r2 * (K.pow(x, 2) - x) x = x + r3 * (K.pow(x, 2) - x) enhanced_image_1 = x + r4 * (K.pow(x, 2) - x) x = enhanced_image_1 + r5 * (K.pow(enhanced_image_1, 2) - enhanced_image_1) x = x + r6 * (K.pow(x, 2) - x) x = x + r7 * (K.pow(x, 2) - x) enhance_image = x + r8 * (K.pow(x, 2) - x) enhance_image = tf.cast((enhance_image[0, :, :, :] * 255), dtype=np.uint8) enhance_image = Image.fromarray(enhance_image.numpy()) enhance_image = enhance_image.resize(original_size, Image.ANTIALIAS) enhance_image.save(test_file.replace(".bmp", "_rs.bmp"))
def initialize_layers(self): w = Input((2, self.num_latent)) noise = Input((self.res, self.res, 2)) _ = self.call((w, noise))
# Fully connected output layer (classification) x = Dense(n_classes, activation='softmax', kernel_initializer='he_normal')(x) return x # Meta-parameter: amount to reduce feature maps by (compression factor) during transition blocks reduction = 0.5 # Meta-parameter: number of filters in a convolution block within a residual block (growth rate) n_filters = 32 # Meta-parameter: number of residual blocks in each dense group groups = { 121 : [6, 12, 24, 16], # DenseNet 121 169 : [6, 12, 32, 32], # DenseNet 169 201 : [6, 12, 48, 32] } # DenseNet 201 # The input vector inputs = Input(shape=(224, 224, 3)) # The Stem Convolution Group x = stem(inputs, n_filters) # The Learner x = learner(x, groups[121], n_filters, reduction) # Classifier for 1000 classes outputs = classifier(x, 1000) # Instantiate the model model = Model(inputs, outputs)
def initialize_layers(self): inputs = Input((self.num_input_latent, )) _ = self.call(inputs)
def __init__(self, word_embedding, data, use_cudnn_lstm=False, plot_model_architecture=True): self.hidden_units = 300 self.embed_model = word_embedding self.input_dim = word_embedding.embed_dim self.vocab_size = data.vocab_size self.left = data.premise self.right = data.hypothesis self.max_len = data.max_len self.dense_units = 32 self.name = '{}_glove{}_lstm{}_dense{}'.format(str(int(time.time())), self.input_dim, self.hidden_units, self.dense_units) embedding_matrix = np.zeros((self.vocab_size, self.input_dim)) for word, i in data.vocab: embedding_vector = self.embed_model.get_vector(word) if embedding_vector is not None: embedding_matrix[i] = embedding_vector embed = layers.Embedding( input_dim=self.vocab_size, output_dim=self.input_dim, embeddings_initializer=Constant(embedding_matrix), input_length=self.max_len, mask_zero=True, trainable=False) #embed.trainable=False if use_cudnn_lstm: lstm = layers.CuDNNLSTM(self.hidden_units, input_shape=(None, self.input_dim), unit_forget_bias=True, kernel_initializer='he_normal', kernel_regularizer='l2', name='lstm_layer') else: lstm = layers.LSTM(self.hidden_units, input_shape=(None, self.input_dim), unit_forget_bias=True, activation='relu', kernel_initializer='he_normal', kernel_regularizer='l2', name='lstm_layer') left_input = Input(shape=(self.max_len), name='input_1') right_input = Input(shape=(self.max_len), name='input_2') embed_left = embed(left_input) embed_right = embed(right_input) print('embed:', embed_right.shape) left_output = lstm(embed_left) right_output = lstm(embed_right) print('lstm:', right_output.shape) l1_norm = lambda x: 1 - K.abs(x[0] - x[1]) merged = layers.Lambda(function=l1_norm, output_shape=lambda x: x[0], name='L1_distance')([left_output, right_output]) #merged = layers.concatenate([left_output, right_output]) #lstm_2 = layers.LSTM(hidden_units, unit_forget_bias=True, # activation = 'relu', kernel_regularizer='l2', name='lstm_layer2' )(merged) print('merged:', merged.shape) dense_1 = layers.Dense(self.dense_units, activation='relu')(merged) print('dense1:', dense_1.shape) output = layers.Dense(3, activation='softmax', name='output_layer')(dense_1) print('output:', output.shape) self.model = Model(inputs=[left_input, right_input], outputs=output) self.compile()
def ssi_model_13(): input_lips = Input(shape=(5, 64, 64, 1)) lips_conv1_1 = TimeDistributed( Conv2D(16, (3, 3), padding="same", activation="relu"))(input_lips) lips_conv1_2 = TimeDistributed( Conv2D(16, (3, 3), padding="same", activation="relu"))(lips_conv1_1) lips_pooling1 = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(lips_conv1_2) lips_conv2_1 = TimeDistributed( Conv2D(32, (3, 3), padding="same", activation="relu"))(lips_pooling1) lips_conv2_2 = TimeDistributed( Conv2D(32, (3, 3), padding="same", activation="relu"))(lips_conv2_1) lips_pooling2 = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(lips_conv2_2) lips_conv3_1 = TimeDistributed( Conv2D(64, (3, 3), padding="same", activation="relu"))(lips_pooling2) lips_conv3_2 = TimeDistributed( Conv2D(64, (3, 3), padding="same", activation="relu"))(lips_conv3_1) lips_pooling3 = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(lips_conv3_2) lips_conv4_1 = TimeDistributed( Conv2D(128, (3, 3), padding="same", activation="relu"))(lips_pooling3) lips_conv4_2 = TimeDistributed( Conv2D(128, (3, 3), padding="same", activation="relu"))(lips_conv4_1) lips_pooling4 = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(lips_conv4_2) lips_conv5_1 = TimeDistributed( Conv2D(256, (3, 3), padding="same", activation="relu"))(lips_pooling4) lips_conv5_2 = TimeDistributed( Conv2D(256, (3, 3), padding="same", activation="relu"))(lips_conv5_1) lips_pooling5 = TimeDistributed(MaxPooling2D(pool_size=(2, 2)))(lips_conv5_2) input_tongues = Input(shape=(5, 64, 64, 1)) tongues_conv1_1 = TimeDistributed( Conv2D(16, (3, 3), padding="same", activation="relu"))(input_tongues) tongues_conv1_2 = TimeDistributed( Conv2D(16, (3, 3), padding="same", activation="relu"))(tongues_conv1_1) tongues_pooling1 = TimeDistributed( MaxPooling2D(pool_size=(2, 2)))(tongues_conv1_2) tongues_conv2_1 = TimeDistributed( Conv2D(32, (3, 3), padding="same", activation="relu"))(tongues_pooling1) tongues_conv2_2 = TimeDistributed( Conv2D(32, (3, 3), padding="same", activation="relu"))(tongues_conv2_1) tongues_pooling2 = TimeDistributed( MaxPooling2D(pool_size=(2, 2)))(tongues_conv2_2) tongues_conv3_1 = TimeDistributed( Conv2D(64, (3, 3), padding="same", activation="relu"))(tongues_pooling2) tongues_conv3_2 = TimeDistributed( Conv2D(64, (3, 3), padding="same", activation="relu"))(tongues_conv3_1) tongues_pooling3 = TimeDistributed( MaxPooling2D(pool_size=(2, 2)))(tongues_conv3_2) tongues_conv4_1 = TimeDistributed( Conv2D(128, (3, 3), padding="same", activation="relu"))(tongues_pooling3) tongues_conv4_2 = TimeDistributed( Conv2D(128, (3, 3), padding="same", activation="relu"))(tongues_conv4_1) tongues_pooling4 = TimeDistributed( MaxPooling2D(pool_size=(2, 2)))(tongues_conv4_2) tongues_conv5_1 = TimeDistributed( Conv2D(256, (3, 3), padding="same", activation="relu"))(tongues_pooling4) tongues_conv5_2 = TimeDistributed( Conv2D(256, (3, 3), padding="same", activation="relu"))(tongues_conv5_1) tongues_pooling5 = TimeDistributed( MaxPooling2D(pool_size=(2, 2)))(tongues_conv5_2) cc = concatenate([lips_pooling5, tongues_pooling5]) flat_layer = Flatten()(cc) fc1 = Dense(736, activation="linear")(flat_layer) mymodel = Model([input_lips, input_tongues], fc1) return mymodel
from tensorflow.keras.layers import Conv2D from tensorflow.keras.models import Sequential from tensorflow.keras.layers import BatchNormalization, Conv2D, Activation, Dense, GlobalAveragePooling2D, MaxPooling2D, ZeroPadding2D, Add from tensorflow.keras import Input from tensorflow.keras.models import Model, load_model # number of classes K = 4 input_tensor = Input(shape=(224, 224, 3), dtype='float32', name='input') def conv1_layer(x): x = ZeroPadding2D(padding=(3, 3))(x) x = Conv2D(64, (7, 7), strides=(2, 2))(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = ZeroPadding2D(padding=(1, 1))(x) return x def con2_layer(x): x = MaxPooling2D((3, 3), 2)(x) shortcut = x for i in range(3): if (i == 0): x = Conv2D(64, (1, 1), strides=(1, 1), padding='valid')(x) x = BatchNormalization()(x) x = Activation('relu')(x)
def mobilenet_model(class_num, input_shape=(32, 32, 3)): inputs = Input(shape=input_shape) outputs = body(inputs, class_num) return Model(inputs, outputs)
def main(): results_dir = "./last_experiments-10" if not os.path.exists(results_dir): os.makedirs(results_dir) logging.basicConfig(filename=os.path.join(results_dir, 'results.log'), level=logging.INFO) experiments = {} # n_samples = [100, 1000] n_samples = [100] # n_informative = [1.0, 0.5] n_informative = [1.0] n_features = [1000, 2000, 3000, 5000, 10000] # 100000 # n_features = [100, 300, 1000, 2000, 3000] # 100000 for ns in n_samples: # for nf in n_features: for ni in n_informative: # if ni <= nf: # print(f'{nf} {ni}') experiments[f's_{ns}_i_{ni}'] = [ns, ni] list_acc_base = [] list_acc_dual = [] list_acc_deep_base = [] list_acc_deep_dual = [] list_acc_kmeans = [] ns_count = [] nf_count = [] ni_count = [] ds_name = [] bar_position = 0 progress_bar = tqdm(experiments.items(), position=bar_position) for j, (dataset, data) in enumerate(progress_bar): progress_bar.set_description("Analysis of dataset: %s" % dataset) ns, ni = data progress_bar_2 = tqdm(n_features, position=1, desc="Number of features") for nf in progress_bar_2: # Xc = np.matmul(X, X.T) # V = np.linalg.eig(Xc)[1] # np.matmul(V.T, X).shape # plt.figure() # plt.plot(np.arange(len(E[0])), E[0]) # plt.show() # return ni2 = int(nf * ni) k = int(ns/10) # epochs = 300 epochs = 2000 lr_base = 0.002 lr_dual = 0.001 lr_deep_dual = 0.002 lmb_dual = 0 # 0.01 lmb_base = 0 # 0.01 # repetitions = 10 repetitions = 1 acc_base = [] acc_dual = [] acc_kmeans = [] kmeans_losses = [] base_loss_Q = [] deep_base_loss_Q = [] dual_loss_Q = [] deep_dual_loss_Q = [] steps = [] progress_bar_3 = tqdm(range(repetitions), position=1, desc="Iterations") for i in progress_bar_3: random.seed = i np.random.seed(i) tf.random.set_seed(i) X, y = make_classification(n_samples=ns, n_features=nf, class_sep=10, n_informative=ni2, n_redundant=0, hypercube=True, n_classes=2, n_clusters_per_class=1, random_state=i) X = StandardScaler().fit_transform(X) u, s, vh = np.linalg.svd(X) print(f'ns: {ns} | nf: {nf} | max s: {np.max(s)} - min s: {np.min(s)}') continue # Base print("\n\nBase Model") inputs = Input(shape=(nf,), name='input') model = BaseModel(n_features=nf, k_prototypes=k, deep=False, inputs=inputs, outputs=inputs) optimizer = tf.keras.optimizers.Adam(learning_rate=lr_base) model.compile(optimizer=optimizer) # model.layers[1].summary() model.fit(X, y, epochs=epochs, verbose=False) x_pred = model.predict(X) prototypes = model.base_model.weights[-1].numpy() accuracy = score(X, prototypes, y) print("Accuracy", accuracy) list_acc_base.append(accuracy) base_loss_Q.extend(model.loss_) print("Loss", model.loss_[-1], "\n") # # Deep base # inputs = Input(shape=(nf,), name='input') # model = BaseModel(n_features=nf, k_prototypes=k, inputs=inputs, outputs=inputs) # optimizer = tf.keras.optimizers.Adam(learning_rate=lr_base) # model.compile(optimizer=optimizer) # model.summary() # model.fit(X, y, epochs=epochs) # x_pred = model.predict(X) # prototypes = model.base_model.weights[-1].numpy() # accuracy = score(X, prototypes, y) # print("Accuracy", accuracy) # list_acc_deep_base.append(accuracy) # deep_base_loss_Q.extend(model.loss_) # Dual print("Dual Model") inputs = Input(shape=(nf,), name='input') model = DualModel(n_samples=ns, k_prototypes=k, deep=False, inputs=inputs, outputs=inputs) optimizer = tf.keras.optimizers.Adam(learning_rate=lr_dual) model.compile(optimizer=optimizer) # model.layers[1].summary() model.fit(X, y, epochs=epochs, verbose=False) x_pred = model.predict(X) prototypes = model.dual_model.predict(x_pred.T) accuracy = score(X, prototypes, y) print("Accuracy", accuracy) list_acc_dual.append(accuracy) dual_loss_Q.extend(model.loss_) print("Loss", model.loss_[-1], "\n") # Deep dual print("Deep Dual Model") inputs = Input(shape=(nf,), name='input') model = DualModel(n_samples=ns, k_prototypes=k, deep=True, inputs=inputs, outputs=inputs) optimizer = tf.keras.optimizers.Adam(learning_rate=lr_deep_dual) model.compile(optimizer=optimizer) # model.layers[1].summary() model.fit(X, y, epochs=epochs, verbose=False) x_pred = model.predict(X) prototypes = model.dual_model.predict(x_pred.T) accuracy = score(X, prototypes, y) print("Accuracy", accuracy) list_acc_deep_dual.append(accuracy) deep_dual_loss_Q.extend(model.loss_) print("Loss", model.loss_[-1], "\n") # k-Means print("k-Means") _, has_samples = compute_graph(x_pred, prototypes, return_has_sampels=True) k1 = np.sum(has_samples) model_km = KMeans(n_clusters=k1, init='random', random_state=i).fit(X) prototypes = model_km.cluster_centers_.T loss = quantization(X, prototypes).numpy().astype('float32') kmeans_losses.extend(len(model.loss_) * [loss]) accuracy = score(X, prototypes.astype('float32'), y) print("Accuracy", accuracy) list_acc_kmeans.append(accuracy) print("Loss", loss, "\n") steps.extend(np.arange(0, epochs)) ns_count.append(ns) nf_count.append(nf) ni_count.append(ni) ds_name.append(f'S {ns} - I {ni}') # Clearing tf session to cancel previous models tf.keras.backend.clear_session() losses_Q = pd.DataFrame({ 'epoch': steps, 'base': base_loss_Q, 'dual': dual_loss_Q, # 'deep-base': deep_base_loss_Q, 'deep-dual': deep_dual_loss_Q, 'kmeans': kmeans_losses, }) losses_Q.to_pickle( os.path.join(results_dir, f'dataframe_losses_#s-{ns}_%i-{ni * 100}_#f-{nf}.pickle')) sns.set_style('whitegrid') plt.figure(figsize=[6, 4]) sns.lineplot('epoch', 'base', data=losses_Q, label='base', ci=99) sns.lineplot('epoch', 'dual', data=losses_Q, label='dual', ci=99) # sns.lineplot('epoch', 'deep-base', data=losses_Q, label='deep-base', ci=99) sns.lineplot('epoch', 'deep-dual', data=losses_Q, label='deep-dual', ci=99) sns.lineplot('epoch', 'kmeans', data=losses_Q, label='kmeans', ci=99) plt.yscale('log', basey=10) # plt.ylim(bottom=0, top=1000000) plt.ylabel('Q') plt.title(f'{dataset}_f_{nf}') plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2), ncol=4) plt.tight_layout() plt.savefig(os.path.join(results_dir, f'{dataset}_f_{nf}_loss_q.png')) plt.savefig(os.path.join(results_dir, f'{dataset}_f_{nf}_loss_q.pdf')) plt.show() # # Add accuracies of iterations for current dataset # list_acc_base += acc_base # list_acc_dual += acc_dual # list_acc_kmeans += acc_kmeans accuracies = pd.DataFrame({ 'number_sample': ns_count, 'number_feature': nf_count, 'number_info_feature': ni_count, 'base': list_acc_base, 'dual': list_acc_dual, # 'deep-base': list_acc_deep_base, 'deep-dual': list_acc_deep_dual, 'kmeans': list_acc_kmeans, }) # Saving dataframe to disk in order to recover data if needed accuracies.to_pickle(os.path.join(results_dir, f'dataframe_accuracy_#s-{ns}_%i-{ni*100}_#f-{n_features}.pickle')) sns.set_style('whitegrid') plt.rc('font', size=12) plt.figure(figsize=[6, 4]) sns.lineplot('number_feature', 'base', data=accuracies, label='GBC', ci=99) sns.lineplot('number_feature', 'dual', data=accuracies, label='DGBC', ci=99) # sns.lineplot('number_feature', 'deep-base', data=accuracies, label='deep-base', ci=99) sns.lineplot('number_feature', 'deep-dual', data=accuracies, label='Deep-DBGC', ci=99) sns.lineplot('number_feature', 'kmeans', data=accuracies, label='k-Means', ci=99) plt.xscale('log', basex=10) plt.xticks(n_features) plt.ylabel('Accuracy') plt.title(f'Accuracies on Blobs #s: {ns}, %i: {ni * 100}') plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2), ncol=4) plt.tight_layout() plt.savefig(os.path.join(results_dir, f'accuracies_Blobs_#s-{ns}_%i-{ni * 100}_#f-{n_features}.png')) plt.savefig(os.path.join(results_dir, f'accuracies_Blobs_#s-{ns}_%i-{ni * 100}_#f-{n_features}.pdf')) plt.show()
################################################################################################### # model building hidden_sizes = list(map(int, args.hidden_sizes.split(','))) args.pool_size = min(gen.xshape[0] // 3, args.pool_size) loss, out_activation = args.loss, args.out_activation loss = 'binary_crossentropy' if loss == 'bce' else loss loss = 'categorical_crossentropy' if loss == 'cce' else loss loss = 'sparse_categorical_crossentropy' if loss == 'spcce' else loss out_activation = 'sigmoid' if loss == 'binary_crossentropy' else args.out_activation out_activation = 'softmax' if 'categorical_crossentropy' in loss else out_activation out_activation = 'tanh' if loss == 'pnl' else out_activation # expected input batch shape: (N, T, F) if len(gen.xshape) == 3: o = i = Input((None, gen.xshape[2])) else: o = i = Input((None,)) o = Embedding(args.input_dim, args.embed_dim)(o) if os.getenv('USE_TFLITE', '0') == '1' or args.layer == 'MLP': i.set_shape((args.batch_size, *i.shape[1:])) if args.layer == 'MLP': o = Flatten()(o) for (l, h) in enumerate(hidden_sizes): return_sequences = l + 1 < len(hidden_sizes) or gen.out_seq if args.layer == 'MLP': o = MLP(h, dropout=args.dropout, use_batch_norm=args.use_batch_norm)(o) elif args.layer == 'Conv': o = Conv(h, dropout=args.dropout, use_batch_norm=args.use_batch_norm, return_sequences=return_sequences)(o) elif args.layer == 'ResNet':
def define_gan(self): self.generator_aux = Generator( self.hidden_dim).build(input_shape=(self.seq_len, self.n_seq)) self.supervisor = Supervisor( self.hidden_dim).build(input_shape=(self.hidden_dim, self.hidden_dim)) self.discriminator = Discriminator( self.hidden_dim).build(input_shape=(self.hidden_dim, self.hidden_dim)) self.recovery = Recovery( self.hidden_dim, self.n_seq).build(input_shape=(self.hidden_dim, self.hidden_dim)) self.embedder = Embedder( self.hidden_dim).build(input_shape=(self.seq_len, self.n_seq)) X = Input(shape=[self.seq_len, self.n_seq], batch_size=self.batch_size, name='RealData') Z = Input(shape=[self.seq_len, self.n_seq], batch_size=self.batch_size, name='RandomNoise') #-------------------------------- # Building the AutoEncoder #-------------------------------- H = self.embedder(X) X_tilde = self.recovery(H) self.autoencoder = Model(inputs=X, outputs=X_tilde) #--------------------------------- # Adversarial Supervise Architecture #--------------------------------- E_Hat = self.generator_aux(Z) H_hat = self.supervisor(E_Hat) Y_fake = self.discriminator(H_hat) self.adversarial_supervised = Model(inputs=Z, outputs=Y_fake, name='AdversarialSupervised') #--------------------------------- # Adversarial architecture in latent space #--------------------------------- Y_fake_e = self.discriminator(E_Hat) self.adversarial_embedded = Model(inputs=Z, outputs=Y_fake_e, name='AdversarialEmbedded') # --------------------------------- # Synthetic data generation # --------------------------------- X_hat = self.recovery(H_hat) self.generator = Model(inputs=Z, outputs=X_hat, name='FinalGenerator') # -------------------------------- # Final discriminator model # -------------------------------- Y_real = self.discriminator(H) self.discriminator_model = Model(inputs=X, outputs=Y_real, name="RealDiscriminator") # ---------------------------- # Init the optimizers # ---------------------------- self.autoencoder_opt = Adam(learning_rate=self.lr) self.supervisor_opt = Adam(learning_rate=self.lr) self.generator_opt = Adam(learning_rate=self.lr) self.discriminator_opt = Adam(learning_rate=self.lr) self.embedding_opt = Adam(learning_rate=self.lr) # ---------------------------- # Define the loss functions # ---------------------------- self._mse = MeanSquaredError() self._bce = BinaryCrossentropy()
[demandX_train, supplyX_train] = np.load('train.npz')['X'] [demandY_train, supplyY_train] = np.load('train.npz')['Y'] factor_train = np.load('train.npz')['factor'] demand_aux_train = np.load('train.npz')['auxiliary'][:, :, :, :1] supply_aux_train = np.load('train.npz')['auxiliary'][:, :, :, 1:] [demandX_test, supplyX_test] = np.load('test.npz')['X'] [demandY_test, supplyY_test] = np.load('test.npz')['Y'] factor_test = np.load('test.npz')['factor'] demand_aux_test = np.load('test.npz')['auxiliary'][:, :, :, :1] supply_aux_test = np.load('test.npz')['auxiliary'][:, :, :, 1:] timestep = 3 size = 16 dim = 4 * 4 * 16 input_demand = Input(shape=(None, size, size, 1)) demand_encoder = encoder(input_demand) demand_reshape = TimeDistributed(Dropout(0.3))(demand_encoder) demand_reshape = TimeDistributed(Flatten())(demand_reshape) demand_reshape = Reshape((timestep, dim))(demand_reshape) input_supply = Input(shape=(None, size, size, 1)) supply_encoder = encoder(input_supply) supply_reshape = TimeDistributed(Dropout(0.3))(supply_encoder) supply_reshape = TimeDistributed(Flatten())(supply_reshape) supply_reshape = Reshape((timestep, dim))(supply_reshape) combine_demand_supply = concatenate([demand_reshape, supply_reshape]) lstm = LSTM(dim, return_sequences=0, input_shape=(timestep, dim * 2))(combine_demand_supply)
def ResNet50(include_top=True, weights='imagenet', input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 244)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') img_input = Input(shape=input_shape) if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model