inputs = Input(shape=input_shape, name='encoder_input') x = Dense(intermediate_dim_1, activation='relu')(inputs) x = Dense(intermediate_dim_2, activation='relu')(x) x = Dense(intermediate_dim_3, activation='relu')(x) x = Dense(intermediate_dim_4, activation='relu')(x) z_mean = Dense(latent_dim, name='z_mean')(x) z_log_var = Dense(latent_dim, name='z_log_var')(x) # use reparameterization trick to push the sampling out as input # note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var]) # instantiate encoder model encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') encoder.summary() plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True) # build decoder model latent_inputs = Input(shape=(latent_dim,), name='z_sampling') x = Dense(intermediate_dim_4, activation='relu')(x) x = Dense(intermediate_dim_3, activation='relu')(x) x = Dense(intermediate_dim_2, activation='relu')(x) x = Dense(intermediate_dim_1, activation='relu')(latent_inputs) outputs = Dense(original_dim, activation='sigmoid')(x) # instantiate decoder model decoder = Model(latent_inputs, outputs, name='decoder') decoder.summary() plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True) # instantiate VAE model
def test_create_search_space(): """Generate a random neural network from the search_space definition. """ from random import random from tensorflow.keras.utils import plot_model import tensorflow as tf import numpy as np search_space = create_search_space() ops = [ 9, 1, 30, 1, 1, 28, 1, 1, 0, 24, 0, 0, 1, 23, 0, 1, 0, 27, 1, 1, 1, 18, 0, 1, 1, 2, 0, 1, 1, 14, 1, 1, 0, 20, 1, 0, 1, ] print(ops) print("Search space size: ", search_space.size) print( f"This search_space needs {len(ops)} choices to generate a neural network." ) search_space.set_ops(ops) model = search_space.create_model() model.summary() plot_model(model, to_file="sampled_neural_network.png", show_shapes=True) print("The sampled_neural_network.png file has been generated.")
featurewise_std_normalization=False, rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.1, horizontal_flip=True) # %% Load model and compile model = MiniXception((*IMAGE_SIZE, 1), 7) model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) # model.summary() plot_model(model, to_file="images/model_plot.png", show_shapes=True, show_layer_names=True) # %% Callbacks model_checkpoint = ModelCheckpoint( TRAINED_FER_MODELS_PATH + "/mini_xception.{val_accuracy:.2f}-{epoch:02d}.hdf5", monitor="val_accuracy", mode="max", verbose=1, save_best_only=True) early_stop = EarlyStopping("val_loss", patience=PATIENCE) reduce_lr = ReduceLROnPlateau("val_loss", factor=0.2, patience=PATIENCE // 4,
help='Restore saved model weights') parser.add_argument( '--eval', default=False, action='store_true', help='Evaluate a pre trained model. Must indicate weights file.') parser.add_argument('--crop', type=int, default=4, help='Pixels to crop from the image') parser.add_argument('--plot-model', default=False, action='store_true', help='Plot all network models') args = parser.parse_args() # build backbone backbone = vgg.VGG(vgg.cfg['F']) backbone.model.summary() # instantiate IIC object iic = IIC(args, backbone.model) if args.plot_model: plot_model(backbone.model, to_file="model-vgg.png", show_shapes=True) plot_model(iic.model, to_file="model-iic.png", show_shapes=True) if args.eval: iic.load_weights() iic.eval() elif args.train: iic.train()
def plot(self, path: str = "mobiledetectnet_plot.png"): from tensorflow.keras.utils import plot_model plot_model(self, to_file=path, show_shapes=True)
def MT_model(perm): #def funx1(i,maxL): #if i<maxL: #return Input(shape=(fpSize,),name=f"MRg_{i}") #elif i<2*maxL-3 and i!=maxL+2: # elif maxL<=i< 2*maxL: #return Input(shape=(FpLen,),name=f"Ml_{i}") #else: # return Input(shape=(FpLen1,),name=f"Sq_{i}") def funx1(i,maxL): if i<maxL: return Input(shape=(FpLen,),name=f"Mol_{i}") elif maxL<=i<2*maxL: return Input(shape=(FpLen1,),name=f"S2q_{i}") else: return Input(shape=(767,),name=f"Mfp2q_{i}") x2 = [funx1(i,maxL) for i in range(2*maxL)] xs=[] xs1=[] xs2=[] for i in range(maxL): xs.append(x2[perm[i]]) xs.append(x2[maxL+perm[i]]) # xs.append(x2[2*maxL+i]) x2_1=Concatenate()([xs[i] for i in range(2*maxL)]) for i in range(4): x2_1 = Dense(112, activation='relu',kernel_initializer=my_init)(x2_1) x2_1 = Dense(256, activation='linear',kernel_initializer=my_init)(x2_1) # list1=[112,112,112,112] # #list2=[112,112,112,112] # pairwise_model = pl.PairwiseModel( # (256,), pl.repeat_layers(Dense, list1, name="hidden",activation='relu'), name="pairwise_model" # ) # perm_encoder = pl.PermutationalEncoder(pairwise_model, 2*maxL, name="permutational_encoder") # perm_layer = pl.PermutationalLayer1(perm_encoder, name="permutational_layer") # outputs = perm_layer.model(xs) # perm_layer4 = pl.PermutationalLayer1( # pl.PermutationalEncoder( # pl.PairwiseModel((112,), pl.repeat_layers(Dense, [256], activation="linear")), 2*maxL # ), # name="permutational_layer4", # ) # outputs = perm_layer4.model(outputs) # #outputs = Add()(outputs) # #outputs1 = Add()(outputs1) # outputs = maximum(outputs) #outputs1 = maximum(outputs1) #outputs2 = maximum(outputs2) #outputs = average(outputs) # outputs1 = average(outputs1) # outputs2 = average(outputs2) # output_3 = Concatenate()([outputs,outputs1,outputs2]) output_3 = x2_1 #output_3 = outputs output_3 = Dense(100, activation='relu',kernel_initializer=my_init)(output_3) output_Loss=Dense(17,name='Loss_output',activation='softmax',kernel_initializer=my_init)(output_3) #output_Loss=Dense(17,name='Loss_output',activation='sigmoid',kernel_initializer=my_init)(output_3) model=Model(inputs=x2,outputs=output_Loss) model.compile(loss={'Loss_output':'categorical_crossentropy'}, #model.compile(loss={'Loss_output':'binary_crossentropy'}, optimizer=Adam(lr=0.00276, beta_1=0.9, beta_2=0.999, epsilon=1e-8), loss_weights = {'Loss_output':1.} # optimizer=Adam(lr=0.00005, beta_1=0.9, beta_2=0.999, epsilon=1e-8), loss_weights = {'Loss_output':1.} ,metrics=['accuracy'] ) model.summary() plot_model(model, 'Config3Mod.png', show_shapes=True) return model
dense_network = [1760, 500, 500] for nodes in dense_network[:-1]: net = tf.keras.layers.Dense(units=nodes)(net) output = [100, 100] mag_scale_factor = 100 phase_scale_factor = 1200 * np.pi mag = tf.keras.layers.Lambda(lambda x: x * phase_scale_factor, name='magnitude')( tf.keras.layers.Dense(units=output[0])(net)) phase = tf.keras.layers.Lambda(lambda x: x * phase_scale_factor, name='phase')( tf.keras.layers.Dense(units=output[1])(net)) keras_model = tf.keras.Model(inputs=spectra16, outputs=[mag, phase]) RMSprop = tf.keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0) keras_model.compile(optimizer=RMSprop, loss={'magnitude': 'mean_squared_error', 'phase': 'mean_squared_error'}, loss_weights=[1., 1.]) direct = './multilayer_cnn_3' filename = direct+'/'+'model.png' plot_model(keras_model, to_file=filename, show_shapes=True) filename = direct+'/'+'model.json' f = open(filename,'w') f.write(keras_model.to_json()) f.close()
def drawModel(self,model,path, method_name, model_name): writepath = path+'/'+method_name+'/'+model_name+'/model.png' os.makedirs(os.path.dirname(writepath), exist_ok=True) plot_model(model, to_file=writepath)
def ResNet(stack_fn, preact, use_bias, model_name='resnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, batch_size=16, pth_hist='', att_type='', **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. # Arguments stack_fn: a function that returns output tensor for the stacked residual blocks. preact: whether to use pre-activation or not (True for ResNetV2, False for ResNet and ResNeXt). use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. 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, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels. 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 not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') 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') if att_type not in ['baseline', 'SE', 'BAM', 'CBAM', 'Retarget']: raise ValueError( 'Custom Attention Module of required type is required to train' 'custom models') if input_shape != (224, 224, 3): raise ValueError('Image dimesions need to be of the size 224 x 224') # Determine proper input shape img_input = layers.Input(shape=input_shape, batch_size=batch_size) bn_axis = 3 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv')(x) if preact is False: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad')(x) x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x, att_type=att_type) if preact is True: x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='post_bn')(x) x = layers.Activation('relu', name='post_relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) x = layers.Dense(classes, activation='softmax', name='probs')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) x = layers.Dense(classes, activation='softmax')(x) inputs = img_input # Create model. model = Model(inputs, x, name=model_name) # Load weights. if (weights == 'imagenet') and (model_name in WEIGHTS_HASHES): if include_top: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels.h5' file_hash = WEIGHTS_HASHES[model_name][0] else: file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_notop.h5' file_hash = WEIGHTS_HASHES[model_name][1] weights_path = utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) by_name = True model.load_weights(weights_path, by_name=by_name) elif weights is not None: model.load_weights(weights, by_name=by_name) if pth_hist != '': plot_model(model, to_file=os.path.join(pth_hist, 'model.png'), dpi=300) return model
continue model_size += bytes_per_parameter * sum(tensor_size(w) for w in m.weights) if isinstance(m, Conv2D) or isinstance(m, DepthwiseConv2D): inference_cost += tensor_size(m.output) // int(m.output.shape[-1]) * tensor_size(m.weights[0]) if m.bias is not None: inference_cost += tensor_size(m.output) elif isinstance(m, (Add, Multiply)): inference_cost += sum(tensor_size(w) for w in m.input[1:]) elif isinstance(m, Dense): inference_cost += tensor_size(m.input) * m.units if m.bias is not None: inference_cost += tensor_size(m.output) # Wrong --- doesn't support parallel branches correctly inputs = [m.input] if not isinstance(m.input, list) else m.input outputs = [m.output] if not isinstance(m.output, list) else m.output mem_usage = sum(tensor_size(w) for w in inputs + outputs) print(m.name, mem_usage) peak_memory_usage = max(peak_memory_usage, mem_usage) return peak_memory_usage, model_size, inference_cost if __name__ == "__main__": from tensorflow.keras.utils import plot_model model = get_efficientnet_bz(2, (128, 128, 3)) model.summary() stats = compute_model_stats(model) plot_model(model, "model.png") print(stats, stats <= (250_000, 250_000, 60_000_000))
) exc_MODEL.trainable=False global_average_layer = tf.keras.layers.GlobalAveragePooling2D() prediction_layer = tf.keras.layers.Dense(23,activation='softmax') model = tf.keras.Sequential([ exc_MODEL, global_average_layer, prediction_layer ]) # print the summary of model model.summary() # visualize the model and save as "model_plot.png" plot_model(model, to_file='model_plot_exception.png', show_shapes=True, show_layer_names=True) #================================================================================= # ================================================================================ def recall(y_target, y_pred): # clip(t, clip_value_min, clip_value_max) : clip_value_min~clip_value_max 이외 가장자리를 깎아 낸다 # round : 반올림한다 y_target_yn = K.round(K.clip(y_target, 0, 1)) # 실제값을 0(Negative) 또는 1(Positive)로 설정한다 y_pred_yn = K.round(K.clip(y_pred, 0, 1)) # 예측값을 0(Negative) 또는 1(Positive)로 설정한다 # True Positive는 실제 값과 예측 값이 모두 1(Positive)인 경우이다 count_true_positive = K.sum(y_target_yn * y_pred_yn) # (True Positive + False Negative) = 실제 값이 1(Positive) 전체 count_true_positive_false_negative = K.sum(y_target_yn)
ae_history = autoenc.fit( x=x_train, y=x_train, batch_size=64, epochs=EPOCHS, validation_data=(x_val, x_val), callbacks=[EarlyStopping(patience=5, restore_best_weights=True)], verbose=0) plot_nn_metrics(ae_history) # Save the trained weights of the autoencoder autoenc.save_weights(r'./Logs/autoencoder.h5') # Save the plot of the autoencoder plot_model(autoenc, to_file=r'./Logs/autoencoder.png', expand_nested=True, show_shapes=True) # %% t-SNE # Creates and TSNE model and plots it # Adapted from https://www.kaggle.com/jeffd23/visualizing-word-vectors-with-t-sne def tsne_plot(data, labels, annotate=False): products = [] vector = [] for l, v in zip(labels, data): products.append(l) vector.append(v) tsne_model = TSNE(perplexity=40,
TrafficGen = TrafficGenerator(config['max_steps'], config['penetration_rate']) Visualization = Visualization(path, dpi=96) #VANILLA MODEL if config['uses_reccurent_network'] == False: # online model used for training Model = VanillaTrainModel(config['batch_size'], config['learning_rate'], output_dim=config['num_actions'], state_shape=state_shape) Model._model.summary() plot_model(Model._model, 'my_first_model_with_shape_info.png', show_shapes=True) #target model, only used for predictions. regularly the values of Model are copied into TargetModel TargetModel = VanillaTrainModel(config['batch_size'], config['learning_rate'], output_dim=config['num_actions'], state_shape=state_shape) Memory = NormalMemory(config['memory_size_max'], config['memory_size_min']) Simulation = VanillaTrainSimulation( Model, TargetModel, Memory, TrafficGen, sumo_cmd, config['gamma'], config['max_steps'], config['green_duration'], config['yellow_duration'], config['num_actions'],
def plot_architecture(self): plot_model(self.model, to_file='{0}.png'.format(self.name))
model.add(Conv2D(128, 2, activation='relu')) model.add(MaxPooling2D(2)) model.add(Dropout(0.2)) model.add(GlobalAveragePooling2D()) model.add(Dense(10, activation='softmax')) model.compile(loss='sparse_categorical_crossentropy', optmizer=Adam(lr=1e-3, decay=1e-5), metrics=['sparse_categorical_accuracy']) model.summary(line_length=150) plot_model(model, to_file=f'{BASE_DIR}\\model.png', show_shapes=True, dpi=200, expand_nested=True) mc = ModelCheckpoint(f'{BASE_DIR}\\model.h5', save_best_only=True, monitor='val_loss') es = EarlyStopping(patience=30, monitor='val_loss') history = model.fit(x_train, y_train, batch_size=32, epochs=150, validation_split=0.2, callbacks=[mc, es])
layer.trainable = False for layer in model.layers[limit_layer:]: layer.trainable = True # Add last layer for categories model.add(Dense(len(class_names), activation = "softmax")) # Save model summary model.summary() with open(os.path.join(save_dir,"model_summary.txt"), "w") as file: with redirect_stdout(file): model.summary() # Plot model architecture and save it as .png try: rankdir = "TB" # TB: vertical; LR: horizontal plot_model(model, to_file = os.path.join(save_dir,"model_plot.png"), show_shapes=True, show_layer_names = True, rankdir = rankdir) except: print("Unable to plot model.") pass ############################################################################################ # INPUT TRAIN DATASET ############################################################################################ # CONFIGURATION ImageDataGenerator class_mode="categorical" shuffle=True seed = 1234 # AUGMENTATION ##############
import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "" #import tensorflow #import keras from tensorflow.keras.models import load_model from tensorflow.keras.utils import plot_model import sys #import numpy as np model_filename = sys.argv[1] model = load_model(model_filename) pic_filename = model_filename + ".png" plot_model(model, to_file=pic_filename, show_shapes=True) print(model.summary())
def build_model(self): """ build VAE model """ input_shape = (self.image_size, self.image_size, self.nchannel) # build encoder model inputs = Input(shape=input_shape, name='encoder_input') x = inputs filters = self.nfilters kernel_size = self.kernel_size for i in range(self.nlayers): # filters *= 2 x = Conv2D(filters=filters, kernel_size=kernel_size, activation='relu', strides=1, padding='same')(x) # shape info needed to build decoder model shape = K.int_shape(x) # generate latent vector Q(z|X) x = Flatten()(x) x = Dense(self.inter_dim, activation='relu')(x) z_mean = Dense(self.latent_dim, name='z_mean')(x) z_log_var = Dense(self.latent_dim, name='z_log_var')(x) # use reparameterization trick to push the sampling out as input z = Lambda(self.sampling, output_shape=(self.latent_dim, ), name='z')([z_mean, z_log_var]) # build decoder model latent_inputs = Input(shape=(self.latent_dim, ), name='z_sampling') x = Dense(shape[1] * shape[2] * shape[3], activation='relu')(latent_inputs) x = Reshape((shape[1], shape[2], shape[3]))(x) for i in range(self.nlayers): x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, activation='relu', strides=1, padding='same')(x) # filters //= 2 outputs = Conv2DTranspose(filters=input_shape[2], kernel_size=kernel_size, activation='sigmoid', padding='same', name='decoder_output')(x) # instantiate encoder model self.encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') self.encoder.summary() plot_model(self.encoder, to_file=os.path.join(self.save_dir, 'encoder_model.png'), show_shapes=True) # instantiate decoder model self.decoder = Model(latent_inputs, outputs, name='decoder') self.decoder.summary() plot_model(self.decoder, to_file=os.path.join(self.save_dir, 'decoder_model.png'), show_shapes=True) # instantiate VAE model outputs = self.decoder(self.encoder(inputs)[2]) self.vae = Model(inputs, outputs, name='vae') # VAE loss terms w/ KL divergence def vae_loss(inputs, outputs): xent_loss = metrics.binary_crossentropy(K.flatten(inputs), K.flatten(outputs)) xent_loss *= self.image_size * self.image_size kl_loss = 1 + z_log_var * 2 - K.square(z_mean) - K.exp( z_log_var * 2) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 vae_loss = K.mean(xent_loss + kl_loss) return vae_loss optimizer = optimizers.rmsprop(lr=self.learn_rate) self.vae.compile(loss=vae_loss, optimizer=optimizer) self.vae.summary() plot_model(self.vae, to_file=os.path.join(self.save_dir, 'vae_model.png'), show_shapes=True) # save model architectures self.model_dir = os.path.join(self.save_dir, 'models') os.makedirs(self.model_dir, exist_ok=True) print('saving model architectures to', self.model_dir) with open(os.path.join(self.model_dir, 'arch_vae.json'), 'w') as file: file.write(self.vae.to_json()) with open(os.path.join(self.model_dir, 'arch_encoder.json'), 'w') as file: file.write(self.encoder.to_json()) with open(os.path.join(self.model_dir, 'arch_decoder.json'), 'w') as file: file.write(self.decoder.to_json())
# normalize pixel values x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 # define model model = Sequential() model.add( Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=in_shape)) model.add(MaxPool2D((2, 2))) model.add(Flatten()) model.add(Dense(100, activation='relu', kernel_initializer='he_uniform')) model.add(Dropout(0.5)) model.add(Dense(n_classes, activation='softmax')) print(model.summary()) plot_model(model, 'model.png', show_shapes=True) plt.show() # define loss and optimizer model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # fit the model model.fit(x_train, trainy, epochs=10, batch_size=128, verbose=2) # evaluate the model loss, acc = model.evaluate(x_test, testy, verbose=2) print('Accuracy: %.3f' % acc) # make a prediction image = x_train[0] yhat = model.predict(asarray([image])) print('Predicted: class=%d' % argmax(yhat))
conv, flow = EncodeBlock(flow, out_channel, train=train) conv_snapshots.append(conv) flow = conv_snapshots[encode_depth - 1] # Decode. for i in range(encode_depth - 2, -1, -1): out_channel = GetOutChannel(i) flow = DecodeBlock(flow, conv_snapshots[i], out_channel, train=train) # Outputs. outputs = Conv2D(2, (1, 1), activation='relu', padding='same')(flow) outputs = Softmax()(outputs) # Compile model. model = Model(inputs=inputs, outputs=outputs) opt = Adam() opt = tf.train.experimental.enable_mixed_precision_graph_rewrite(opt) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) return model if __name__ == '__main__': unet = Unet((512, 512, 1), 5) plot_model(unet, show_shapes=True, show_layer_names=False, to_file='./logs/test_model.png')
def _build_model(self, load_file, auto_latest=False): if self._cfg('agent', 'non_sequnetial', default=False): self._prediction_network = self._build_NS_Model( input=Input(shape=( (2, self._cfg('arena', 'width'), self._cfg('arena', 'height'), self._cfg('arena', 'length'), len(self._inputs)) if self. _cfg('agent', 'use_full_observation', default=True) else ( 2, self._cfg('agent', 'observation_width'), self._cfg('agent', 'observation_height'), self._cfg('agent', 'observation_width'), len(self._inputs)))), layers_list=self._cfg('agent', 'layers')) else: self._prediction_network = Sequential() # Take in the blueprint as desired and the state of the world, in the same shape as the blueprint self._prediction_network.add( InputLayer(input_shape=( (2, self._cfg('arena', 'width'), self._cfg('arena', 'height'), self._cfg('arena', 'length'), len(self._inputs)) if self. _cfg('agent', 'use_full_observation', default=True) else ( 2, self._cfg('agent', 'observation_width'), self._cfg('agent', 'observation_height'), self._cfg('agent', 'observation_width'), len(self._inputs))))) # Now, load layers from config file and build them out: for layer_str in self._cfg('agent', 'layers'): # Don't try to process comments if layer_str.lstrip()[0] != '#': # Dangerous to use eval, but convenient for our purposes. new_layer = eval( layer_str.format( arena_width=self._cfg('arena', 'width'), arena_height=self._cfg('arena', 'height'), arena_length=self._cfg('arena', 'length'), observation_width=self._cfg('agent', 'observation_width', default=0), observation_height=self._cfg('agent', 'observation_height', default=0), num_inputs=len(self._cfg('inputs')), num_actions=len(self._cfg('actions')))) self._prediction_network.add(new_layer) if self._cfg('agent', 'auto_final_layer', default=True): # Output one-hot encoded action self._prediction_network.add( Dense(len(self._cfg('actions')), activation='softmax')) # Otherwise, user should provide such a layer. Model will fail later if they didn't. self._prediction_network.compile(loss='mse', optimizer='adam', metrics=[]) self.start_episode = 0 if load_file is not False: self._prediction_network, self.start_episode = std_load( self._name, self._prediction_network, load_file=load_file, auto_latest=auto_latest) plot_model(self._prediction_network, show_shapes=True, to_file='{}_model.png'.format(self._name)) self._target_network = clone_model(self._prediction_network) self._target_network.build(self._prediction_network.input_shape)
def summary(self, input_shape): x_in = Input(shape=input_shape, name='X') summary = Model(inputs=x_in, outputs=self.call(x_in), name=self.name) return plot_model(summary, show_shapes=True) # forward pass
1], data_vector_train[:, shape[1] - 1] y_train = to_categorical(y_train) INPUT_DIMS = X_train.shape[1] from tensorflow.keras.utils import plot_model os.environ['PATH'] = os.environ['PATH'] + ';' + os.environ[ 'CONDA_PREFIX'] + r"\Library\bin\graphviz" print('training') model = get_aleatoric_uncertainty_model(epochs, X_train, y_train, input_shape=X_train.shape, T=T, D=D) plot_model(model, to_file='graph.png', show_shapes=True) # ====================================================================================================================== # Testing with cloud gaps data_test, data_vector_test, data_ind_test, feat_keep = preprocessing( data_path, img, pctl, feat_list_new, test=False) perm_index = feat_keep.index('GSW_perm') flood_index = feat_keep.index('flooded') data_vector_test[data_vector_test[:, perm_index] == 1, flood_index] = 0 # Remove flood water that is perm water data_vector_test = np.delete(data_vector_test, perm_index, axis=1) # Remove perm water column shape = data_vector_test.shape X_test, y_test = data_vector_test[:, 0:shape[1] - 1], data_vector_test[:, shape[1] - 1] y_test = to_categorical(y_test)
model.add(Dense(128,activation='relu')) model.add(Dropout(0.5)) model.add(BatchNormalization()) model.add(Dense(64,activation='relu')) model.add(Dropout(0.5)) model.add(BatchNormalization()) model.add(Dense(32,activation='relu')) model.add(Dropout(0.2)) model.add(Dense(4, activation='sigmoid')) model.compile(optimizer=tf.keras.optimizers.SGD(lr=1e-6, decay= 0.01, momentum=0.99 , nesterov=True), loss='categorical_crossentropy',metrics=['accuracy']) model.summary() # To plot the Neural Model plot_model(model, to_file='Images/model_plot.png', show_shapes=True, show_layer_names=False) # To compute weights for the model based on the training set weights train_y_int= [y.argmax() for y in train_y] compute_weights = class_weight.compute_class_weight('balanced',np.unique(train_y_int),train_y_int) weights = dict(enumerate(compute_weights)) # Cat: {0: 'agree', 1: 'disagree', 2: 'discuss', 3: 'unrelated'} print(' The computed weights of the model will be set as:', '\n' , weights) # To set weights and fit the model (this takes some times) history = model.fit(train_x, train_y, batch_size=32, epochs=50, validation_data=(val_x, val_y), class_weight=weights) print('\n','*'*20,'Model trained','*'*20) # To save sequentioal model model.save('data/sequential_model') print('Sequential model has been saved')
padding='valid', activation='relu')) model.add(layers.AveragePooling2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(120, activation='relu')) model.add(layers.Dense(84, activation='relu')) model.add(layers.Dense(10, activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # 모델 정보 model.summary() # 모델 그래프 이미지로 저장 plot_model(model, show_shapes=True, to_file='LeNet5.png') # tensorboard 모니터링을 위한 callback 함수 정의 callbacks = [ keras.callbacks.TensorBoard(log_dir='LeNet_log', histogram_freq=1) ] # Model fitting, training history = model.fit(train_images, train_labels, epochs=30, batch_size=64, validation_split=0.1, callbacks=callbacks) # 학습된 model 저장
def plot(self, base_path='./logs/schemas'): plot_model(self.model, to_file=base_path + '/' + self.name + '.png')
x2 = tf.keras.layers.Conv2D(filters=32,kernel_size=(2,2),padding="same", activation=tf.nn.relu)(x1) x3 = tf.keras.layers.Conv2D(filters=16,kernel_size=(3,3),padding="same", activation=tf.nn.relu)(x2) x4 = tf.keras.layers.MaxPool2D(pool_size=(2,2),strides=(2,2))(x3) x5 =tf.keras.layers.Flatten()(x4) x6 =tf.keras.layers.Dropout(0.2)(x5) x7 = tf.keras.layers.Dense(72, activation=tf.nn.relu)(x6) out0 = tf.keras.layers.Dense(6, activation=tf.nn.softmax)(x7) model = tf.keras.Model(inputs=in0, outputs=out0) model.summary() from tensorflow.keras.optimizers import Adam model.compile(tf.keras.optimizers.SGD(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy']) # Visualize from tensorflow.keras.utils import plot_model plot_model(model, to_file='my_model0124.png', show_shapes=True, show_layer_names=True) # Training hist=model.fit(train, steps_per_epoch=100, validation_data=valid, validation_steps=50, epochs=12,verbose=2) # Results plt.plot(hist.history['accuracy']) plt.plot(hist.history['val_accuracy']) plt.title('history: the CNN model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'test'], loc='lower right') plt.show() plt.plot(hist.history['loss']) plt.plot(hist.history['val_loss']) plt.title('history: the CNN model loss')
feature_maps=feature_maps, depth=depth, drop_values=dropout_values, spatial_dropout=spatial_dropout, batch_norm=batch_normalization, k_init=kernel_init, loss_type=loss_type, optimizer=optimizer, lr=learning_rate_value, n_classes=n_classes) # Check the network created model.summary(line_length=150) os.makedirs(char_dir, exist_ok=True) model_name = os.path.join(char_dir, "model_plot_" + job_identifier + ".png") plot_model(model, to_file=model_name, show_shapes=True, show_layer_names=True) h5_file = os.path.join( h5_dir, weight_files_prefix + previous_job_weights + '_' + str(args.run_id) + '.h5') if load_previous_weights == False: results = model.fit(train_generator, validation_data=val_generator, validation_steps=math.ceil(X_val.shape[0] / batch_size_value), steps_per_epoch=steps_per_epoch_value, epochs=epochs_value, callbacks=[earlystopper, checkpointer, time_callback]) print("Loading model weights from h5_file: {}".format(h5_file))
def evaluate(data_name, univariate): print('Data: ', data_name) train_x, train_y, test_x, test_y = load_data(data_name, univariate=univariate) # n_steps = train_x.iloc[0][0].shape[0] n_features = train_x.columns.shape[0] X_train, X_test, n_steps = flatten_ts(train_x, test_x) X_train, X_test = rnn_reshape(X_train, X_test, n_steps // n_features, n_features) encoder, decoder = TRepNet(n_steps // n_features, n_features, activation='elu') model = keras.models.Sequential([encoder, decoder]) plot_model(encoder, to_file='encoder.png', show_shapes=True, show_layer_names=True) plot_model(decoder, to_file='decoder.png', show_shapes=True, show_layer_names=True) start_time = time.time() model.compile(loss="mae", optimizer=keras.optimizers.Nadam(lr=0.001, clipnorm=1.), metrics=['mae']) history = model.fit(X_train, X_train, epochs=500, batch_size=16, validation_data=[X_test, X_test], callbacks=[es], verbose=0, shuffle=False) # Codings codings_train = encoder.predict(X_train) codings_test = encoder.predict(X_test) # # RF # rf_clf.fit(codings_train, train_y) # pred = rf_clf.predict(codings_test) # rf_scores = {'accuracy': accuracy_score(test_y, pred), 'f1': f1_score(test_y, pred, average='weighted')} # print('RF >>', rf_scores) # SVM svm_clf = SVC(random_state=7, gamma='scale') nb_classes = np.unique(train_y).shape[0] train_size = codings_train.shape[0] if train_size // nb_classes < 5 or train_size < 50: svm_clf.fit(codings_train, train_y) else: grid_search = GridSearchCV( svm_clf, {'C': [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000, np.inf]}, cv=5, iid=False, n_jobs=-1) if train_size <= 10000: grid_search.fit(codings_train, train_y) else: codings_train, _, train_y, _ = train_test_split(codings_train, train_y, train_size=10000, random_state=7, stratify=train_y) grid_search.fit(codings_train, train_y) svm_clf = grid_search.best_estimator_ svm_clf.fit(codings_train, train_y) # svm_clf.fit(codings_train, train_y) pred = svm_clf.predict(codings_test) duration = time.time() - start_time svm_scores = { 'accuracy': accuracy_score(test_y, pred), 'f1': f1_score(test_y, pred, average='weighted') } print('SVM >>', svm_scores) # # 1-NN # knn_clf.fit(codings_train, train_y) # pred = knn_clf.predict(codings_test) # knn_scores = {'accuracy': accuracy_score(test_y, pred), 'f1': f1_score(test_y, pred, average='weighted')} # print('1-NN >>', knn_scores) # # MLP # mlp_clf.fit(codings_train, train_y) # pred = mlp_clf.predict(codings_test) # mlp_scores = {'accuracy': accuracy_score(test_y, pred), 'f1': f1_score(test_y, pred, average='weighted')} # print('MLP >>', mlp_scores) # SOTA Results print('*' * 10) print( 'InceptionTime:', inception[inception['dataset_name'] == data_name]['accuracy'].values[0] if len(inception[inception['dataset_name'] == data_name] ['accuracy'].values) >= 1 else 'N/A') print( 'ResNet:', resnet_ucr[resnet_ucr['dataset_name'] == data_name]['accuracy']. values[0] if len(resnet_ucr[resnet_ucr['dataset_name'] == data_name] ['accuracy'].values) >= 1 else 'N/A') print( 'ResNet:', resnet_uea[resnet_uea['dataset_name'] == data_name]['accuracy']. values[0] if len(resnet_uea[resnet_uea['dataset_name'] == data_name] ['accuracy'].values) >= 1 else 'N/A') print( 'ResNet:', resnet_mts[resnet_mts['dataset_name'] == data_name]['accuracy']. values[0] if len(resnet_mts[resnet_mts['dataset_name'] == data_name] ['accuracy'].values) >= 1 else 'N/A') print( 'HIVE-COTE:', hive_cote[hive_cote['dataset_name'] == data_name]['HIVE-COTE']. values[0] if len(hive_cote[hive_cote['dataset_name'] == data_name] ['HIVE-COTE'].values) >= 1 else 'N/A') print( 'DTW:', dtw_uea[dtw_uea['dataset_name'] == data_name]['DTW'].values[0] if len(dtw_uea[dtw_uea['dataset_name'] == data_name]['DTW'].values) == 1 else 'N/A') print('*' * 10) results.append({ 'dataset': data_name, 'dim': codings_train.shape[1], # 'RF-ACC': rf_scores['accuracy'], 'SVM-ACC': svm_scores['accuracy'], # '1NN-ACC': knn_scores['accuracy'], # 'MLP-ACC': mlp_scores['accuracy'], # 'RF-F1': rf_scores['f1'], 'SVM-F1': svm_scores['f1'], # '1NN-F1': knn_scores['f1'], # 'MLP-F1': mlp_scores['f1'], 'duration (sec)': duration })
def train(save_model=False): train_families, test_families, train_positive_relations, test_positive_relations = process_data( set_seed=set_seed_datagen, seed=seed_datagen) # constructs training data pipeline train_dataset = make_triplet_dataset(train_families, train_positive_relations) # constructs test data pipeline test_dataset = make_triplet_dataset(test_families, test_positive_relations) # dictionaries containing metadata for plotting during training loss_plot_settings = { 'variables': { 'loss': 'Training loss', 'val_loss': 'Validation loss' }, 'title': 'Losses', 'ylabel': 'Epoch Loss', 'last_50': False } roc_plot_settings = { 'variables': { 'ROC_custom_metric': 'Training AUC', 'val_ROC_custom_metric': 'Validation AUC' }, 'title': 'ROC - AUC', 'ylabel': 'AUC', 'last_50': False } probabilities_plot_settings = { 'variables': { 'pos_prob': 'Training positive probabilities', 'neg_prob': 'Training negative probabilities', 'val_pos_prob': 'Validation positive probabilities', 'val_neg_prob': 'Validation negative probabilities' }, 'title': 'Probabilities', 'ylabel': 'Probabilities', 'last_50': False } distances_plot_settings = { 'variables': { 'pos_dist': 'Training positive distances', 'neg_dist': 'Training negative distances', 'val_pos_dist': 'Validation positive distances', 'val_neg_dist': 'Validation negative distances' }, 'title': 'Embedding Distances', 'ylabel': 'Embedding distances', 'last_50': False } # folder path for creating the folder that will contain the training data logs_path = 'Training Plots/Training...' # creation of callback objects losses_and_roc_plot_callback = CallbackPlot( folder_path=logs_path, plots_settings=(loss_plot_settings, roc_plot_settings), title='Losses and ROC', share_x=True) probs_and_dists_plot_callback = CallbackPlot( folder_path=logs_path, plots_settings=(probabilities_plot_settings, distances_plot_settings), title='Probabilities and Embedding Distances', share_x=True) save_logs_callback = CallbackSaveLogs(folder_path=logs_path) # creation of model model = make_facenet_based_model() # creation of folder for saving the training data create_folder(logs_path) # train the model history = model.fit(x=train_dataset, validation_data=test_dataset, validation_steps=8, steps_per_epoch=steps_per_epoch, epochs=epochs, callbacks=[ losses_and_roc_plot_callback, probs_and_dists_plot_callback, save_logs_callback ]) # saves model if save_model: print('\nSaving model...') model.save(logs_path + '/trained_model.h5', save_format='h5') model.save_weights('{}/saved_weights.h5'.format(logs_path)) print('\nModel successfully saved') # saves model layout print('\nSaving model layout') plot_model(model, to_file=logs_path + '/model.png', rankdir='LR', show_shapes=True) print('\nModel layout successfully saved') timestamp_end = datetime.now().strftime('%d-%b-%y -- %H:%M:%S') # renames the training folder with the end-of-training timestamp root, _ = os.path.split(logs_path) os.rename(logs_path, root + '/' + 'Training Session - ' + timestamp_end) return model, history