def get_model(): '''stack ensemble by NN model ''' input_layer = Input(shape=get_ensemble_inputShape()) layer = Dense(units=128, activation='relu')(input_layer) layer = BatchNormalization()(layer) layer = Dropout(0.3)(layer) output_layer = Dense(6, activation='sigmoid')(layer) model = Model(inputs=input_layer, outputs=output_layer) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['acc']) return model
class JointSequentialIntentModel(IntentExtractionModel): """ Joint Intent classification and Slot tagging Model """ def __init__(self): super(JointSequentialIntentModel, self).__init__() def build(self, sentence_length, vocab_size, tag_labels, intent_labels, token_emb_size=100, tagger_hidden=100, tagger_dropout=0.5, intent_classifier_hidden=100, emb_model_path=None): """ Build the model Args: sentence_length (int): max length of a sentence vocab_size (int): vocabulary size tag_labels (int): number of tag labels intent_labels (int): number of intent labels token_emb_size (int): token embedding vectors size tagger_hidden (int): label tagger LSTM hidden size tagger_dropout (float): label tagger dropout rate intent_classifier_hidden (int): intent LSTM hidden size emb_model_path (str): external embedding model path """ tokens_input, token_emb = self._create_input_embed(sentence_length, emb_model_path is not None, token_emb_size, vocab_size) intent_enc = Bidirectional(LSTM(intent_classifier_hidden))(token_emb) intent_out = Dense(intent_labels, activation='softmax', name='intent_classifier')(intent_enc) intent_vec_rep = RepeatVector(sentence_length)(intent_out) slot_emb = Bidirectional(LSTM(tagger_hidden, return_sequences=True))(token_emb) tagger_features = concatenate([slot_emb, intent_vec_rep], axis=-1) tagger = Bidirectional( LSTM(tagger_hidden, return_sequences=True))(tagger_features) tagger = Dropout(tagger_dropout)(tagger) tagger_out = TimeDistributed( Dense(tag_labels, activation='softmax'), name='slot_tag_classifier')(tagger) self.model = Model(inputs=tokens_input, outputs=[ intent_out, tagger_out]) self.model.compile(optimizer='rmsprop', loss='categorical_crossentropy', loss_weights=[1., 1.], metrics=['categorical_accuracy'])
def build(self, sentence_length, vocab_size, tag_labels, token_emb_size=100, encoder_depth=1, decoder_depth=1, lstm_hidden_size=100, encoder_dropout=0.5, decoder_dropout=0.5, emb_model_path=None): """ Build the model Args: sentence_length (int): max sentence length vocab_size (int): vocabulary size tag_labels (int): number of tag labels token_emb_size (int, optional): token embedding vector size encoder_depth (int, optional): number of encoder LSTM layers decoder_depth (int, optional): number of decoder LSTM layers lstm_hidden_size (int, optional): LSTM layers hidden size encoder_dropout (float, optional): encoder dropout decoder_dropout (float, optional): decoder dropout emb_model_path (str, optional): external embedding model path """ tokens_input, token_emb = self._create_input_embed(sentence_length, emb_model_path is not None, token_emb_size, vocab_size) benc_in = token_emb assert encoder_depth > 0, 'Encoder depth must be > 0' for i in range(encoder_depth): bencoder = LSTM(lstm_hidden_size, return_sequences=True, return_state=True, go_backwards=True, dropout=encoder_dropout, name='encoder_blstm_{}'.format(i))(benc_in) benc_in = bencoder[0] b_states = bencoder[1:] benc_h, bene_c = b_states decoder_inputs = token_emb assert decoder_depth > 0, 'Decoder depth must be > 0' for i in range(decoder_depth): decoder = LSTM(lstm_hidden_size, return_sequences=True, name='decoder_lstm_{}'.format(i))(decoder_inputs, initial_state=[benc_h, bene_c]) decoder_inputs = decoder decoder_outputs = Dropout(decoder_dropout)(decoder) decoder_predictions = TimeDistributed( Dense(tag_labels, activation='softmax'), name='decoder_classifier')(decoder_outputs) self.model = Model(tokens_input, decoder_predictions) self.model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
def processing(path, word_index, input_length, x_train): """Processing string array with pretrained vectors. convert an n dimension string array into n * k * m dimension float numpy array. Each k * m array represents a string. k is the input_length which means an upper bound of the string length, for string shorter than k will be pad and longer string will be cropped. m is defined by the pretrained file. Args: path: String, path where the pre trained files stored. word_index: Dictionary, contains word with tokenlized index. input_length: Int, an upper bound of the string length. x_train: String array. Returns: x_train: Numpy array as processed x_train. """ embedding_matrix = load_pretrain(path=path, word_index=word_index) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) backend.set_session(sess) print("generating preprocessing model...") embedding_layer = Embedding(len(word_index) + 1, Constant.EMBEDDING_DIM, weights=[embedding_matrix], input_length=input_length, trainable=False) sequence_input = Input(shape=(input_length,), dtype='int32') embedded_sequences = embedding_layer(sequence_input) model = Model(sequence_input, embedded_sequences) print("converting text to vector...") x_train = model.predict(x_train) del model return x_train
def resnet_50(input_shape): img_input = Input(input_shape) x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(img_input) if input_shape[-1] > 3: x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1_changed')(img_input) x = BatchNormalization(name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(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') print("Loading pretrained weights for Resnet50...") weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', resnet50_padding.WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model = Model(img_input, x) model.load_weights(weights_path, by_name=True) if input_shape[-1] > 3: print("Loading weights for conv1 layer separately for the first 3 channels") conv1_weights = np.zeros((7, 7, input_shape[-1], 64), dtype="float32") resnet_ori = ResNet50(include_top=False, input_shape=(224, 224, 3)) conv1_weights[:, :, :3, :] = resnet_ori.get_layer("conv1").get_weights()[0][:, :, :, :] # random init conv1_weights[:, :, 3:, :] = model.get_layer('conv1_changed').get_weights()[0][:, :, 3:, :] bias = resnet_ori.get_layer("conv1").get_weights()[1] model.get_layer('conv1_changed').set_weights((conv1_weights, bias)) model.get_layer('conv1_changed').name = 'conv1' return model
def build(self): """Builds the network using Keras. """ word_inputs = kl.Input(shape=(None, MAX_WORD_LENGTH+2), dtype="int32") inputs = [word_inputs] word_outputs = self._build_word_cnn(word_inputs) if len(self.word_vectorizers) > 0: additional_word_inputs = [kl.Input(shape=(None, input_dim), dtype="float32") for input_dim, dense_dim in self.word_vectorizers] inputs.extend(additional_word_inputs) additional_word_embeddings = [kl.Dense(dense_dim)(additional_word_inputs[i]) for i, (_, dense_dim) in enumerate(self.word_vectorizers)] word_outputs = kl.Concatenate()([word_outputs] + additional_word_embeddings) outputs, lstm_outputs = self._build_basic_network(word_outputs) compile_args = {"optimizer": ko.nadam(lr=0.002, clipnorm=5.0), "loss": "categorical_crossentropy", "metrics": ["accuracy"]} self.model_ = Model(inputs, outputs) self.model_.compile(**compile_args) if self.verbose > 0: self.model_.summary(print_fn=log.info) return self
def __init__(self, model_file=None): # Build Network Architecture input_shape = Board().encoded_states().shape # (6, 15, 15) inputs = Input(input_shape) shared_net = Sequential([ *ConvBlock(32, input_shape=input_shape), *ConvBlock(64), *ConvBlock(128) ], "shared_net") policy_head = Sequential([ shared_net, *ConvBlock(4, (1, 1), "relu"), Flatten(), Dense(Game["board_size"], kernel_regularizer=l2()), Activation("softmax") ], "policy_head") value_head = Sequential([ shared_net, *ConvBlock(2, (1, 1), "relu"), Flatten(), Dense(64, activation="relu", kernel_regularizer=l2()), Dense(1, kernel_regularizer=l2()), Activation("tanh") ], "value_head") self.model = Model( inputs, [value_head(inputs), policy_head(inputs)] ) if model_file is not None: self.restore_model(model_file)
def layer_test(layer_cls, kwargs={}, input_shape=None, input_dtype=None, input_data=None, expected_output=None, expected_output_dtype=None, fixed_batch_size=False): """Test routine for a layer with a single input tensor and single output tensor. """ # generate input data if input_data is None: assert input_shape if not input_dtype: input_dtype = K.floatx() input_data_shape = list(input_shape) for i, var_e in enumerate(input_data_shape): if var_e is None: input_data_shape[i] = np.random.randint(1, 4) input_data = (10 * np.random.random(input_data_shape)) input_data = input_data.astype(input_dtype) else: if input_shape is None: input_shape = input_data.shape if input_dtype is None: input_dtype = input_data.dtype if expected_output_dtype is None: expected_output_dtype = input_dtype # instantiation layer = layer_cls(**kwargs) # test get_weights , set_weights at layer level weights = layer.get_weights() layer.set_weights(weights) if isinstance(layer, layers.ReflectionPadding2D): layer.build(input_shape) expected_output_shape = layer.compute_output_shape(input_shape) # test in functional API if fixed_batch_size: inp = Input(batch_shape=input_shape, dtype=input_dtype) else: inp = Input(shape=input_shape[1:], dtype=input_dtype) outp = layer(inp) assert K.dtype(outp) == expected_output_dtype # check with the functional API model = Model(inp, outp) actual_output = model.predict(input_data) actual_output_shape = actual_output.shape for expected_dim, actual_dim in zip(expected_output_shape, actual_output_shape): if expected_dim is not None: assert expected_dim == actual_dim if expected_output is not None: assert_allclose(actual_output, expected_output, rtol=1e-3) # test serialization, weight setting at model level model_config = model.get_config() recovered_model = model.__class__.from_config(model_config) if model.weights: weights = model.get_weights() recovered_model.set_weights(weights) _output = recovered_model.predict(input_data) assert_allclose(_output, actual_output, rtol=1e-3) # test training mode (e.g. useful when the layer has a # different behavior at training and testing time). if has_arg(layer.call, 'training'): model.compile('rmsprop', 'mse') model.train_on_batch(input_data, actual_output) # test instantiation from layer config layer_config = layer.get_config() layer_config['batch_input_shape'] = input_shape layer = layer.__class__.from_config(layer_config) # for further checks in the caller function return actual_output
print('Adding new layers...') for layer in model.layers[:5]: layer.trainable = False #Adding custom Layers x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(1024, activation="relu")(x) output = Dense(17, activation="softmax")(x) vgg19_model = Model(model.input, output) vgg19_model.compile(optimizers.SGD(lr=0.0001, momentum=0.9), loss="categorical_crossentropy", metrics=["accuracy"]) #model.fit(X_train, Y_train, batch_size=128, epochs=epochs, verbose=1) # Train the neural network #model.fit(X_train, Y_train, validation_data=(X_test, Y_test), batch_size=batch_size, epochs=epochs, verbose=1) callbacks = EarlyStopping(monitor='val_loss', patience=1, verbose=1, mode='auto') # autosave best Model best_model_file = "data_augmented_weights_vgg19.h5" best_model = ModelCheckpoint(best_model_file, monitor='val_acc', verbose=2, save_best_only=True) # In[16]:
class DAFNetImageCallback(BaseSaveImage): """ Image callback for saving images during DAFNet training. Images are saved in a subfolder with name training_images, created inside the experiment folder. """ def __init__(self, conf, model, data_gen_lb): """ :param conf: configuration object :param model: a DAFNet model :param data_gen_lb: a python iterator of images+masks """ self.conf = conf super(DAFNetImageCallback, self).__init__(conf.folder, model) self._make_dirs(self.folder) self.data_gen_lb = data_gen_lb self.init_models() def _make_dirs(self, folder): self.lr_folder = folder + '/images_lr' if not os.path.exists(self.lr_folder): os.makedirs(self.lr_folder) self.segm_folder = folder + '/images_segm' if not os.path.exists(self.segm_folder): os.makedirs(self.segm_folder) self.rec_folder = folder + '/images_rec' if not os.path.exists(self.rec_folder): os.makedirs(self.rec_folder) self.discr_folder = folder + '/images_discr' if not os.path.exists(self.discr_folder): os.makedirs(self.discr_folder) def init_models(self): self.encoders_anatomy = self.model.Encoders_Anatomy self.reconstructor = self.model.Decoder self.segmentor = self.model.Segmentor self.discr_mask = self.model.D_Mask self.enc_modality = self.model.Enc_Modality self.fuser = self.model.Anatomy_Fuser self.discr_mask = self.model.D_Mask mean = get_net(self.enc_modality, 'z_mean') var = get_net(self.enc_modality, 'z_log_var') self.z_mean = Model(self.enc_modality.inputs, mean.output) self.z_var = Model(self.enc_modality.inputs, var.output) def on_epoch_end(self, epoch=None, logs=None): ''' Plot training images from the real_pool. For SDNet the real_pools will contain images paired with masks, and also unlabelled images. :param epoch: current training epoch :param logs: ''' x_mod1, x_mod2, m_mod1, m_mod2 = next(self.data_gen_lb) image_list = [x_mod1[..., 0:1], x_mod2[..., 0:1]] masks_list = [ m_mod1[..., 0:self.conf.num_masks], m_mod2[..., 0:self.conf.num_masks] ] # we usually plot 4 image-rows. If we have less, it means we've reached the end of the data, so iterate from # the beginning while len(image_list[0]) < 4: x_mod1, x_mod2 = image_list m_mod1, m_mod2 = masks_list x_mod1_2, x_mod2_2, m_mod1_2, m_mod2_2 = next(self.data_gen_lb) image_list = [ np.concatenate([x_mod1[..., 0:1], x_mod1_2[..., 0:1]], axis=0), np.concatenate([x_mod2[..., 0:1], x_mod2_2[..., 0:1]], axis=0) ] masks_list = [ np.concatenate([ m_mod1[..., 0:self.conf.num_masks], m_mod1_2[..., 0:self.conf.num_masks] ], axis=0), np.concatenate([ m_mod2[..., 0:self.conf.num_masks], m_mod2_2[..., 0:self.conf.num_masks] ], axis=0) ] self.plot_latent_representation(image_list, epoch) self.plot_segmentations(image_list, masks_list, epoch) self.plot_reconstructions(image_list, epoch) self.plot_discriminator_outputs(image_list, masks_list, epoch) def plot_latent_representation(self, image_list, epoch): """ Plot a 4-row image, where the first column shows the input image and the following columns each of the 8 channels of the spatial latent representation. :param image_list: a list of 4-dim arrays of images, one for each modality :param epoch : the epoch number """ x_list, s_list = [], [] for mod_i in range(len(image_list)): images = image_list[mod_i] x = utils.data_utils.sample(images, nb_samples=4, seed=self.conf.seed) x_list.append(x) # plot S s = self.encoders_anatomy[mod_i].predict(x) s_list.append(s) rows = [ np.concatenate( [x[i, :, :, 0]] + [s[i, :, :, s_chn] for s_chn in range(s.shape[-1])], axis=1) for i in range(x.shape[0]) ] im_plot = np.concatenate(rows, axis=0) scipy.misc.imsave( self.lr_folder + '/mod_%d_s_lr_epoch_%d.png' % (mod_i, epoch), im_plot) # plot Z enc_modality_inputs = [ self.encoders_anatomy[mod_i].predict(images), images ] z, _ = self.enc_modality.predict(enc_modality_inputs) means = self.z_mean.predict(enc_modality_inputs) variances = self.z_var.predict(enc_modality_inputs) means = np.var(means, axis=0) variances = np.mean(np.exp(variances), axis=0) with open(self.lr_folder + '/z_means.csv', 'a+') as f: f.writelines( ', '.join([str(means[i]) for i in range(means.shape[0])]) + '\n') with open(self.lr_folder + '/z_vars.csv', 'a+') as f: f.writelines(', '.join( [str(variances[i]) for i in range(variances.shape[0])]) + '\n') # plot deformed anatomies new_anatomies = self.fuser.predict(s_list) s1_def = new_anatomies[0] rows = [ np.concatenate( [x_list[0][i, :, :, 0], x_list[1][i, :, :, 0]] + [s1_def[i, :, :, s_chn] for s_chn in range(s1_def.shape[-1])], axis=1) for i in range(x_list[0].shape[0]) ] im_plot = np.concatenate(rows, axis=0) scipy.misc.imsave(self.lr_folder + '/s1def_lr_epoch_%d.png' % (epoch), im_plot) def plot_segmentations(self, image_list, mask_list, epoch): ''' Plot an image for every sample, where every row contains a channel of the spatial LR and a channel of the predicted mask. :param image_list: a list of 4-dim arrays of images, one for each modality :param masks_list: a list of 4-dim arrays of masks, one for each modality :param epoch: the epoch number ''' x_list, s_list, m_list2 = [], [], [] for mod_i in range(len(image_list)): images = image_list[mod_i] masks = mask_list[mod_i] x = utils.data_utils.sample(images, 4, seed=self.conf.seed) m = utils.data_utils.sample(masks, 4, seed=self.conf.seed) x_list.append(x) m_list2.append(m) assert x.shape[: -1] == m.shape[: -1], 'Incompatible shapes: %s vs %s' % ( str(x.shape), str(m.shape)) s = self.encoders_anatomy[mod_i].predict(x) y = self.segmentor.predict(s) s_list.append(s) rows = [] for i in range(x.shape[0]): y_list = [y[i, :, :, chn] for chn in range(y.shape[-1])] m_list = [m[i, :, :, chn] for chn in range(m.shape[-1])] if m.shape[-1] < y.shape[-1]: m_list += [np.zeros(shape=(m.shape[1], m.shape[2])) ] * (y.shape[-1] - m.shape[-1]) assert len(y_list) == len( m_list), 'Incompatible sizes: %d vs %d' % (len(y_list), len(m_list)) rows += [ np.concatenate([x[i, :, :, 0]] + y_list + m_list, axis=1) ] im_plot = np.concatenate(rows, axis=0) scipy.misc.imsave( self.segm_folder + '/mod_%d_segmentations_epoch_%d.png' % (mod_i, epoch), im_plot) new_anatomies = self.fuser.predict(s_list) pred_masks = [self.segmentor.predict(s) for s in new_anatomies] rows = [] for i in range(x_list[0].shape[0]): for y in pred_masks: y_list = [ y[i, :, :, chn] for chn in range(self.conf.num_masks) ] m_list = [ m_list2[1][i, :, :, chn] for chn in range(self.conf.num_masks) ] assert len(y_list) == len( m_list), 'Incompatible sizes: %d vs %d' % (len(y_list), len(m_list)) rows += [ np.concatenate( [x_list[0][i, :, :, 0], x_list[1][i, :, :, 0]] + y_list + m_list, axis=1) ] im_plot = np.concatenate(rows, axis=0) scipy.misc.imsave( self.segm_folder + '/fused_segmentations_epoch_%d.png' % (epoch), im_plot) def plot_discriminator_outputs(self, image_list, mask_list, epoch): ''' Plot a histogram of predicted values by the discriminator :param image_list: a list of 4-dim arrays of images, one for each modality :param masks_list: a list of 4-dim arrays of masks, one for each modality :param epoch: the epoch number ''' s_list = [ enc.predict(x) for enc, x in zip(self.encoders_anatomy, image_list) ] s_list += self.fuser.predict(s_list) # s2_def, fused_s2 = self.fuser.predict(reversed(s_list)) # s_list += [s1_def, fused_s1] s = np.concatenate(s_list, axis=0) m = np.concatenate(mask_list, axis=0) pred_m = self.segmentor.predict(s) m = m[..., 0:self.discr_mask.input_shape[-1]] pred_m = pred_m[..., 0:self.discr_mask.input_shape[-1]] m = utils.data_utils.sample(m, nb_samples=4) pred_m = utils.data_utils.sample(pred_m, nb_samples=4) plt.figure() for i in range(4): plt.subplot(4, 2, 2 * i + 1) m_allchn = np.concatenate( [m[i, :, :, chn] for chn in range(m.shape[-1])], axis=1) plt.imshow(m_allchn, cmap='gray') plt.xticks([]) plt.yticks([]) plt.title('Pred: %.3f' % self.discr_mask.predict( m[i:i + 1]).reshape(1, -1).mean(axis=1)) plt.subplot(4, 2, 2 * i + 2) pred_m_allchn_img = np.concatenate( [pred_m[i, :, :, chn] for chn in range(pred_m.shape[-1])], axis=1) plt.imshow(pred_m_allchn_img, cmap='gray') plt.xticks([]) plt.yticks([]) plt.title( 'Pred: %.3f' % self.discr_mask.predict(pred_m).reshape(1, -1).mean(axis=1)) plt.tight_layout() plt.savefig(self.discr_folder + '/discriminator_mask_epoch_%d.png' % epoch) plt.close() def plot_reconstructions(self, image_list, epoch): """ Plot two images showing the combination of the spatial and modality LR to generate an image. The first image uses the predicted S and Z and the second samples Z from a Gaussian. :param image_list: a list of 2 4-dim arrays of images :param epoch: the epoch number """ x_list, s_list = [], [] for mod_i in range(len(image_list)): images = image_list[mod_i] x = utils.data_utils.sample(images, nb_samples=4, seed=self.conf.seed) x_list.append(x) # S + Z -> Image s = self.encoders_anatomy[mod_i].predict(x) s_list.append(s) im_plot = self.get_rec_image(x, s) scipy.misc.imsave( self.rec_folder + '/mod_%d_rec_epoch_%d.png' % (mod_i, epoch), im_plot) new_anatomies = self.fuser.predict(s_list) s1_def = new_anatomies[0] im_plot = self.get_rec_image(x_list[1], s1_def) scipy.misc.imsave( self.rec_folder + '/s1def_rec_epoch_%d.png' % (epoch), im_plot) def get_rec_image(self, x, s): z, _ = self.enc_modality.predict([s, x]) gaussian = NormalDistribution() y = self.reconstructor.predict([s, z]) y_s0 = self.reconstructor.predict([s, np.zeros(z.shape)]) all_bkg = np.concatenate([ np.zeros(s.shape[:-1] + (s.shape[-1] - 1, )), np.ones(s.shape[:-1] + (1, )) ], axis=-1) y_0z = self.reconstructor.predict([all_bkg, z]) y_00 = self.reconstructor.predict([all_bkg, np.zeros(z.shape)]) z_random = gaussian.sample(z.shape) y_random = self.reconstructor.predict([s, z_random]) rows = [ np.concatenate([ x[i, :, :, 0], y[i, :, :, 0], y_random[i, :, :, 0], y_s0[i, :, :, 0] ] + [ self.reconstructor.predict([get_s0chn(k, s), z])[i, :, :, 0] for k in range(s.shape[-1] - 1) ] + [y_0z[i, :, :, 0], y_00[i, :, :, 0]], axis=1) for i in range(x.shape[0]) ] header = utils.image_utils.makeTextHeaderImage( x.shape[2], ['X', 'rec(s,z)', 'rec(s,~z)', 'rec(s,0)'] + ['rec(s0_%d, z)' % k for k in range(s.shape[-1] - 1)] + ['rec(0, z)', 'rec(0,0)']) im_plot = np.concatenate([header] + rows, axis=0) im_plot = np.clip(im_plot, -1, 1) return im_plot
2**min(convolutional_layers, 2))) * int( (training_set.getImageDimensions()[1] / 2**min(convolutional_layers, 2))) * convolutional_filters_per_layer[-1] encoded = encoder(input_img, convolutional_layers, convolutional_filter_size, convolutional_filters_per_layer, dropout_rate) flatten = layers.Flatten()(encoded) embedding = layers.Dense(embedding_dimension, name='embedding')(flatten) fc = layers.Dense(fully_connected_size, name="fully_connected")(embedding) dropout = layers.Dropout(rate=dropout_rate, name="final_dropout")(fc) output_layer = layers.Dense(training_labels.num_classes(), activation='softmax', name="output")(dropout) classifier = Model(input_img, output_layer) classifier.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam()) # Print it's summary classifier.summary() # Load encoder weights classifier.load_weights(autoencoder_weights_file, by_name=True) # Train phase 1: Only fully connected layer weights # Make encoder layers non trainable for layer in classifier.layers[0:encoder_layers(convolutional_layers)]: layer.trainable = False
game.add_tokens(11, 12, 12, 11) game.display_board() game.clear_board() p1 = Player(game) p2 = Player(game, 'Dean') ## # Quelques débuts de parties s'arrêtant au tour 10, avec vérification de puissance 4 for k in range(5): game.clear_board() for i in range(10): p1.play() p2.play() game.display_board() game.check_connect4() ## # Début de model, rien d'important input_board = Input(shape=(4, 4, 4, 1)) x = Conv3D(8, (2, 2, 2), activation='relu', padding='same')(input_board) x = MaxPooling3D((2, 2, 2), padding='same')(x) x = Conv3D(64, (2, 2, 2), activation='relu', padding='same')(x) x = MaxPooling3D((2, 2, 2), padding='same')(x) x = Softmax(16)(x) autoencoder = Model(input_board, x)
labels, test_size=0.25, random_state=42) lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) baseModel = VGG16(weights='imagenet', include_top=False, input_tensor=Input(shape=(224, 224, 3))) headModel = FCHeadNet.build(baseModel, len(classNames), 256) model = Model(inputs=baseModel.input, outputs=headModel) print("layers.len", len(baseModel.layers)) for layer in baseModel.layers: layer.trainable = False opt = RMSprop(lr=0.001) model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"]) model.fit_generator(aug.flow(trainX, trainY, batch_size=32), validation_data=(testX, testY), epochs=25, steps_per_epoch=len(trainX) // 32,
# %% from keras import Input, layers from keras import Model input_tensor = Input(shape=(64,)) x = layers.Dense(32, activation='relu')(input_tensor) x = layers.Dense(32, activation='relu')(x) output_tensor = layers.Dense(10, activation='softmax')(x) model = Model(input_tensor, output_tensor) model.summary() # %% import numpy as np model.compile(optimizer='rmsprop', loss='categorical_crossentropy') x_train = np.random.random((1000, 64)) y_train = np.random.random((1000, 10)) model.fit(x_train, y_train, epochs=10, batch_size=128) score = model.evaluate(x_train, y_train)
def return_deepU(shape=(436, 1024, 3)): I1 = Input(shape=shape) I2 = Input(shape=shape) act = "tanh" I = Concatenate(axis=-1)([I1, I2]) I = BatchNormalization()(I) z1 = Conv2D(16, (3, 3), padding='same', activation=act)(I) z1 = BatchNormalization()(z1) z1 = Conv2D(16, (3, 3), padding='same', activation=act)(z1) z1 = BatchNormalization()(z1) z1 = Conv2D(16, (3, 3), padding='same', activation=act)(z1) z1 = BatchNormalization()(z1) z2 = Conv2D(16, (3, 3), strides=(2, 2), padding='same', activation=act)(z1) z2 = BatchNormalization()(z2) # z3 = Conv2D(32,(3,3), padding='same',activation=act)(z2) # z3 = BatchNormalization()(z3) # z3 = Conv2D(32,(3,3), padding='same',activation=act)(z2)#z2 # z3 = BatchNormalization()(z3) z3 = Conv2D(32, (3, 3), padding='same', activation=act)(z2) #z2 z3 = BatchNormalization()(z3) z4 = Conv2D(32, (3, 3), strides=(2, 2), padding='same', activation=act)(z3) z4 = BatchNormalization()(z4) z4 = Conv2D(64, (3, 3), padding='same', activation=act)(z4) z4 = BatchNormalization()(z4) z4 = Conv2D(64, (3, 3), padding='same', activation=act)(z4) z4 = BatchNormalization()(z4) z4 = Conv2D(64, (3, 3), padding='same', activation=act)(z4) z4 = BatchNormalization()(z4) z5 = Conv2D(32, (3, 3), padding='same', activation=act)(z4) z5 = BatchNormalization()(z5) z5 = Conv2D(32, (3, 3), padding='same', activation=act)(z5) z5 = BatchNormalization()(z5) z5 = Conv2D(32, (3, 3), padding='same', activation=act)(z5) z5 = BatchNormalization()(z5) z6 = Conv2DTranspose(32, (3, 3), strides=(2, 2), padding='same')(z5) z7 = Concatenate(axis=-1)([z6, z3]) z8 = BatchNormalization()(z7) z8 = Conv2D(64, (3, 3), padding='same', activation=act)(z8) z8 = BatchNormalization()(z8) z8 = Conv2D(32, (3, 3), padding='same', activation=act)(z8) z8 = BatchNormalization()(z8) z8 = Conv2D(32, (3, 3), padding='same', activation=act)(z8) z8 = BatchNormalization()(z8) z8 = Conv2D(32, (3, 3), padding='same', activation=act)(z8) z8 = BatchNormalization()(z8) z9 = Conv2D(16, (3, 3), padding='same', activation=act)(z8) z9 = BatchNormalization()(z9) z9 = Conv2D(16, (3, 3), padding='same', activation=act)(z9) z9 = BatchNormalization()(z9) z9 = Conv2D(16, (3, 3), padding='same', activation=act)(z9) z9 = BatchNormalization()(z9) z10 = Conv2DTranspose(16, (3, 3), strides=(2, 2), padding='same')(z9) z11 = Concatenate(axis=-1)([z10, z1]) z12 = BatchNormalization()(z11) z12 = Conv2D(32, (3, 3), padding='same', activation=act)(z12) z12 = BatchNormalization()(z12) z12 = Conv2D(16, (3, 3), padding='same', activation=act)(z12) z12 = BatchNormalization()(z12) z12 = Conv2D(8, (3, 3), padding='same', activation=act)(z12) z12 = BatchNormalization()(z12) z12 = Conv2D(4, (3, 3), padding='same', activation=act)(z12) z12 = BatchNormalization()(z12) z12 = Conv2D(2, (3, 3), padding='same', activation=act)(z12) z12 = BatchNormalization()(z12) z12 = Conv2D(2, (3, 3), padding='same', activation=keras.layers.LeakyReLU(alpha=0.3))(z12) z13 = BatchNormalization()(z12) model = Model(inputs=[I1, I2], outputs=[z13]) model.compile(loss="mse", optimizer='Adam') return model
def prepare(self): print("Setting up CNN v8..") if not os.path.exists(self.cnn_dir): os.makedirs(self.cnn_dir) ImageFile.LOAD_TRUNCATED_IMAGES = True # Rescale all images by 1./255 and apply image augmentation train_datagen = ImageDataGenerator(shear_range=0.2, zoom_range=0.2, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, rescale=1. / 255) validation_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator(rescale=1. / 255) self.train_generator = train_datagen.flow_from_directory( self.train_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode='categorical') self.validation_generator = validation_datagen.flow_from_directory( self.validation_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode='categorical') self.test_generator = test_datagen.flow_from_directory( self.test_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode='categorical') print("Generators are ready, saving class indices") np.save((self.cnn_dir + "class_indices.npy"), self.train_generator.class_indices) self.IMG_SHAPE = (self.img_width, self.img_height, 3) self.num_classes = len(self.train_generator.class_indices) print("Creating transfer train model") # Create the base model from the pre-trained model MobileNet V2 on imagenet data self.base_model = MobileNetV2(input_shape=self.IMG_SHAPE, include_top=False, weights='imagenet') # its already trained, we just use the features self.base_model.trainable = False top_model = Sequential() top_model.add(GlobalAveragePooling2D()) top_model.add( Dense(128, activation='relu', kernel_regularizer=l2(0.01))) top_model.add(Dropout(0.5)) top_model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01))) top_model.add(Dropout(0.5)) top_model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01))) top_model.add(Dropout(0.5)) top_model.add( Dense(self.num_classes, activation='softmax', kernel_regularizer=l2(0.01))) self.model = Model(input=self.base_model.input, output=top_model(self.base_model.output)) self.model.compile(optimizer=RMSprop(lr=self.learning_rate), loss='categorical_crossentropy', metrics=['accuracy']) print("Model created for id %s" % self.id) self.steps_per_epoch = self.train_generator.n // self.batch_size self.validation_steps = self.validation_generator.n // self.batch_size self.test_steps = self.test_generator.n // self.batch_size print("Initializing callbacks") self.callbacks = [ EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=0, mode='auto'), ModelCheckpoint(filepath=(self.cnn_dir + "model-weights.h5"), verbose=1, save_best_only=True), History(), TensorBoard(log_dir="logs/{}".format(self.id)) ] try: self.model.load_weights((self.cnn_dir + "model-weights.h5")) print("Found weights for %s, loading them and continue training" % self.id) except OSError: pass print("Cnn is ready for train")
class Cnn8(object): def __init__(self, pid, params): self.img_width, self.img_height = params['image_width'], params[ 'image_height'] self.id = pid print("Using cnn8 classifier with id ", str(self.id)) self.train_dir = params['train_dir'] self.validation_dir = params['validation_dir'] self.test_dir = params['test_dir'] self.cnn_dir = paths.ROOT_DIR + '/model/' + self.id + '/' self.batch_size = params['batch_size'] self.learning_rate = params['learning_rate'] self.epochs = params['epochs'] self.workers = params['workers'] self.fine_tune_from = params['fine_tune_from'] self.train_generator, self.validation_generator, self.test_generator = None, None, None self.transfer_train_time = params['transfer_train_params'][ 'train_time'] self.tt_acc = params['transfer_train_params']['accuracy'] self.tt_loss = params['transfer_train_params']['loss'] self.fine_tune_time = params['fine_tune_params']['train_time'] self.ft_acc = params['fine_tune_params']['accuracy'] self.ft_loss = params['fine_tune_params']['loss'] # TODO test params def prepare(self): print("Setting up CNN v8..") if not os.path.exists(self.cnn_dir): os.makedirs(self.cnn_dir) ImageFile.LOAD_TRUNCATED_IMAGES = True # Rescale all images by 1./255 and apply image augmentation train_datagen = ImageDataGenerator(shear_range=0.2, zoom_range=0.2, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, rescale=1. / 255) validation_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator(rescale=1. / 255) self.train_generator = train_datagen.flow_from_directory( self.train_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode='categorical') self.validation_generator = validation_datagen.flow_from_directory( self.validation_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode='categorical') self.test_generator = test_datagen.flow_from_directory( self.test_dir, target_size=(self.img_width, self.img_height), batch_size=self.batch_size, class_mode='categorical') print("Generators are ready, saving class indices") np.save((self.cnn_dir + "class_indices.npy"), self.train_generator.class_indices) self.IMG_SHAPE = (self.img_width, self.img_height, 3) self.num_classes = len(self.train_generator.class_indices) print("Creating transfer train model") # Create the base model from the pre-trained model MobileNet V2 on imagenet data self.base_model = MobileNetV2(input_shape=self.IMG_SHAPE, include_top=False, weights='imagenet') # its already trained, we just use the features self.base_model.trainable = False top_model = Sequential() top_model.add(GlobalAveragePooling2D()) top_model.add( Dense(128, activation='relu', kernel_regularizer=l2(0.01))) top_model.add(Dropout(0.5)) top_model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01))) top_model.add(Dropout(0.5)) top_model.add(Dense(64, activation='relu', kernel_regularizer=l2(0.01))) top_model.add(Dropout(0.5)) top_model.add( Dense(self.num_classes, activation='softmax', kernel_regularizer=l2(0.01))) self.model = Model(input=self.base_model.input, output=top_model(self.base_model.output)) self.model.compile(optimizer=RMSprop(lr=self.learning_rate), loss='categorical_crossentropy', metrics=['accuracy']) print("Model created for id %s" % self.id) self.steps_per_epoch = self.train_generator.n // self.batch_size self.validation_steps = self.validation_generator.n // self.batch_size self.test_steps = self.test_generator.n // self.batch_size print("Initializing callbacks") self.callbacks = [ EarlyStopping(monitor='val_loss', min_delta=0, patience=3, verbose=0, mode='auto'), ModelCheckpoint(filepath=(self.cnn_dir + "model-weights.h5"), verbose=1, save_best_only=True), History(), TensorBoard(log_dir="logs/{}".format(self.id)) ] try: self.model.load_weights((self.cnn_dir + "model-weights.h5")) print("Found weights for %s, loading them and continue training" % self.id) except OSError: pass print("Cnn is ready for train") def transfer_train(self): start_time = time.time() print("Transfer training started") self.history = self.model.fit_generator( self.train_generator, steps_per_epoch=self.steps_per_epoch, epochs=self.epochs, workers=self.workers, callbacks=self.callbacks, validation_data=self.validation_generator, validation_steps=self.validation_steps) self.tt_loss, self.tt_acc = self.model.evaluate_generator( self.test_generator, self.test_steps) self.transfer_train_time += time.time() - start_time print("Transfer train ended at: %d sec" % self.transfer_train_time) self.model.save(self.cnn_dir + 'model.h5') self.acc = self.history.history['accuracy'] self.val_acc = self.history.history['val_accuracy'] self.loss = self.history.history['loss'] self.val_loss = self.history.history['val_loss'] self.create_plot('transfer_train', self.acc, self.val_acc, self.loss, self.val_loss) return { 'train_time': self.transfer_train_time, 'accuracy': self.tt_acc, 'loss': self.tt_loss } def create_plot(self, name, acc, val_acc, loss, val_loss): plt.figure(figsize=(8, 8)) plt.subplot(2, 1, 1) plt.plot(acc, label='Training Accuracy') plt.plot(val_acc, label='Validation Accuracy') plt.legend(loc='lower right') plt.ylabel('Accuracy') plt.ylim([min(plt.ylim()), 1]) plt.title('Training and Validation Accuracy') plt.subplot(2, 1, 2) plt.plot(loss, label='Training Loss') plt.plot(val_loss, label='Validation Loss') plt.legend(loc='upper right') plt.ylabel('Cross Entropy') plt.ylim([0, max(plt.ylim())]) plt.title('Training and Validation Loss') plt.savefig(self.cnn_dir + name + ".png") plt.close() def fine_tune(self): start_time = time.time() print("Fine tune started") self.base_model.trainable = True # Let's take a look to see how many layers are in the base model print("Unfreezing %d layer from %d: " % (self.fine_tune_from, len(self.base_model.layers))) # Freeze all the layers before the `fine_tune_from` layer for layer in self.base_model.layers[:self.fine_tune_from]: layer.trainable = False # Recompile model self.model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=2e-5), metrics=['accuracy']) history_fine = self.model.fit_generator( self.train_generator, steps_per_epoch=self.steps_per_epoch, epochs=self.epochs, callbacks=self.callbacks, workers=4, validation_data=self.validation_generator, validation_steps=self.validation_steps) self.ft_loss, self.ft_acc = self.model.evaluate_generator( self.test_generator, self.test_steps) self.model.save(self.cnn_dir + 'model.h5') self.fine_tune_time += time.time() - start_time print("Fine tuning model ended %d" % self.fine_tune_time) self.acc += history_fine.history['accuracy'] self.val_acc += history_fine.history['val_accuracy'] self.loss += history_fine.history['loss'] self.val_loss += history_fine.history['val_loss'] self.create_plot('fine_tune', self.acc, self.val_acc, self.loss, self.val_loss) return { 'train_time': self.fine_tune_time, 'accuracy': self.ft_acc, 'loss': self.ft_loss } def test(self): start_time = time.time() print("Test started")
def make_model_512(): input_img = Input(shape=(512, 512, 3)) x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x=layers.Dropout(0.3)(x) #256 x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x=layers.Dropout(0.3)(x) #128 x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x=layers.Dropout(0.3)(x) #64 x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x=layers.Dropout(0.3)(x) #32 x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x=layers.Dropout(0.3)(x) #16 # x=layers.Dense(16*16*16,activation='relu')(x) # x=layers.Dropout(0.3)(x) # x=layers.Dense(16*16*16,activation='relu')(x) # x=layers.Dense(8*8*16,activation='relu')(x) # encoder_output=layers.Reshape((8,8,16))(x) x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) # x=layers.Dropout(0.3)(x) #8 # x=layers.Conv2D(16,(3,3),activation='relu',padding='same')(x) # encoder_output=layers.MaxPool2D((2,2),padding='same')(x) # # now the decoder part # #decoder_input=Input(shape=(4,4,16)) # x1=layers.Conv2D(16,(3,3),activation='relu',padding='same')(encoder_output) # x1=layers.UpSampling2D((2,2))(x1) x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) # x1=layers.Dropout(0.3)(x1) #16 x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) # x1=layers.Dropout(0.3)(x1) #32 x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) # x1=layers.Dropout(0.3)(x1) #64 x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) # x1=layers.Dropout(0.3)(x1) #128 x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) # x1=layers.Dropout(0.3)(x1) #256 x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.Conv2D(1, (3, 3), activation='relu', padding='same')(x1) # x1=layers.Dropout(0.3)(x1) #512 x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) decoder_output = layers.Conv2D(3, (3, 3), activation='relu', padding='same')(x1) final_model = Model(input_img, decoder_output) final_model.compile(loss='mean_squared_error', optimizer=optimizers.Adagrad(lr=1e-4), metrics=['acc']) return final_model
def make_model_128_embedding(): input_img = Input(shape=(128, 128, 3)) with K.name_scope('Encoder'): with K.name_scope('Convolution_layer_1'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #64 with K.name_scope('Convolution_layer_2'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #32 with K.name_scope('Convolution_layer_3'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #16 with K.name_scope('Convolution_layer_4'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same', name='encoded_output')(x) #x=layers.Dropout(0.3)(x) # x=Round(x.shape,name='Rounder')(x) # x=layers.Embedding(1024,1024)(x) # x=Add()(x) # x=layers.Reshape((8,8,16))(x) x = layers.Dense(512, activation='relu')(x) x = layers.Dense(1024, activation='relu')(x) with K.name_scope('Decoder'): with K.name_scope('DeConvolutiopn_layer_1'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) #x1=layers.Dropout(0.3)(x1) #16 with K.name_scope('DeConvolution_layer_2'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #3 with K.name_scope('DeConvolution_layer_3'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #32 with K.name_scope('DeConvolution_layer_4'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #64 with K.name_scope('DeConvolution_layer_5_with_output'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) decoder_output = layers.Conv2D(3, (3, 3), activation='relu', padding='same', name='final_output')(x1) #128 final_model = Model(input_img, decoder_output) final_model.compile(loss='mean_squared_error', optimizer='adadelta', metrics=['acc']) return final_model
def make_model_128_with_regularizer(): input_img = Input(shape=(128, 128, 3)) with K.name_scope('Encoder'): with K.name_scope('Convolution_layer_1'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #64 with K.name_scope('Convolution_layer_2'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #32 with K.name_scope('Convolution_layer_3'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same')(x) #x=layers.Dropout(0.3)(x) #16 with K.name_scope('Convolution_layer_4'): x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x) x = layers.BatchNormalization()(x) x = layers.MaxPool2D((2, 2), padding='same', name='encoded_output')(x) #x=layers.Dropout(0.3)(x) with K.name_scope('FullyConnectedLayer'): x = layers.Flatten()(x) x = layers.Dense(8 * 8 * 16, activity_regularizer=binary_regularizer)(x) x1 = layers.Reshape((8, 8, 16))(x) with K.name_scope('Decoder'): with K.name_scope('DeConvolution_layer_1'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #16 with K.name_scope('DeConvolution_layer_2'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #3 with K.name_scope('DeConvolution_layer_3'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) x1 = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x1) #x1=layers.Dropout(0.3)(x1) #32 with K.name_scope('DeConvolution_layer_4_with_output'): x1 = layers.UpSampling2D((2, 2), interpolation='bilinear')(x1) x1 = layers.BatchNormalization()(x1) decoder_output = layers.Conv2D(3, (3, 3), activation='relu', padding='same', name='final_output')(x1) #128 final_model = Model(input_img, decoder_output) final_model.compile(loss='mean_squared_error', optimizer='adadelta', metrics=['acc']) return final_model
def build(self, sentence_length, word_length, target_label_dims, word_vocab, word_vocab_size, char_vocab_size, word_embedding_dims=100, char_embedding_dims=25, word_lstm_dims=25, tagger_lstm_dims=100, tagger_fc_dims=100, dropout=0.2, external_embedding_model=None): """ Build a NERCRF model Args: sentence_length (int): max sentence length word_length (int): max word length in characters target_label_dims (int): number of entity labels (for classification) word_vocab (dict): word to int dictionary word_vocab_size (int): word vocabulary size char_vocab_size (int): character vocabulary size word_embedding_dims (int): word embedding dimensions char_embedding_dims (int): character embedding dimensions word_lstm_dims (int): character LSTM feature extractor output dimensions tagger_lstm_dims (int): word tagger LSTM output dimensions tagger_fc_dims (int): output fully-connected layer size dropout (float): dropout rate external_embedding_model (str): path to external word embedding model """ # build word input words_input = Input(shape=(sentence_length,), name='words_input') if external_embedding_model is not None: # load and prepare external word embedding external_emb, ext_emb_size = load_word_embeddings(external_embedding_model) embedding_matrix = np.zeros((word_vocab_size, ext_emb_size)) for word, i in word_vocab.items(): embedding_vector = external_emb.get(word.lower()) if embedding_vector is not None: # words not found in embedding index will be all-zeros. embedding_matrix[i] = embedding_vector # load pre-trained word embeddings into an Embedding layer # note that we set trainable = False so as to keep the embeddings fixed embedding_layer = Embedding(word_vocab_size, ext_emb_size, weights=[embedding_matrix], input_length=sentence_length, trainable=False) else: # learn embeddings ourselves embedding_layer = Embedding(word_vocab_size, word_embedding_dims, input_length=sentence_length) word_embeddings = embedding_layer(words_input) word_embeddings = Dropout(dropout)(word_embeddings) # create word character embeddings word_chars_input = Input(shape=(sentence_length, word_length), name='word_chars_input') char_embedding_layer = Embedding(char_vocab_size, char_embedding_dims, input_length=word_length) char_embeddings = TimeDistributed(char_embedding_layer)(word_chars_input) char_embeddings = TimeDistributed(Bidirectional(LSTM(word_lstm_dims)))(char_embeddings) char_embeddings = Dropout(dropout)(char_embeddings) # create the final feature vectors features = concatenate([word_embeddings, char_embeddings], axis=-1) # encode using a bi-lstm bilstm = Bidirectional(LSTM(tagger_lstm_dims, return_sequences=True))(features) bilstm = Dropout(dropout)(bilstm) after_lstm_hidden = Dense(tagger_fc_dims)(bilstm) # classify the dense vectors crf = CRF(target_label_dims, sparse_target=False) predictions = crf(after_lstm_hidden) # compile the model model = Model(inputs=[words_input, word_chars_input], outputs=predictions) model.compile(loss=crf.loss_function, optimizer='adam', metrics=[crf.accuracy]) self.model = model
elif combine == 5: merge = Maximum()([embedded_sequences, rtwo]) else: merge = Add()([embedded_sequences, rtwo]) gru_kata = Bidirectional(GRU(EMBEDDING_DIM, return_sequences=True), merge_mode=merge_m, weights=None)(merge) crf = CRF(len(label.index) + 1, learn_mode='marginal')(gru_kata) preds = Dense(len(label.index) + 1, activation='softmax')(gru_kata) print "Model Choice:" model_choice = 1 model = Model(inputs=[sequence_input, sequence_input_c], outputs=[crf]) if model_choice == 2: model = Model(inputs=[sequence_input, sequence_input_c], outputs=[preds]) optimizer = 'adagrad' loss = 'categorical_crossentropy' model.summary() model.compile(loss=loss, optimizer=optimizer, metrics=['acc']) # plot_model(model, to_file='model.png') print "|- Optimizer:", optimizer print "|- Loss function:", loss epoch = 3 batch = 8 model.fit([np.array(x_train.padded), np.array(x_train_char)], [np.array(y_encoded)],
def define_model(self, model_config=None): # if model_config is None: # model_config = self.default_config() max_sent_len = self.text_mapper.max_sent_len max_word_len = self.text_mapper.max_word_len word_vocab_size = self.text_mapper.word_mapper.get_vocab_len() char_vocab_size = self.text_mapper.char_mapper.get_vocab_len() chars_input = Input(shape=(max_sent_len, max_word_len), name='chars_input', dtype='int64') char_feats_input = Input( shape=(max_sent_len, max_word_len, self.text_mapper.char_mapper.num_add_feats), name='chars_feats_input', dtype='float32') char_features = char_level_feature_model(chars_input, char_feats_input, max_word_len, char_vocab_size) words_input = Input(shape=(max_sent_len, ), name='words_input', dtype='int64') matrix_shape = self.embedding.embedding_matrix.shape if self.embedding is not None: un_trainable_words_embedding = EmbeddingLayer( input_dim=word_vocab_size, output_dim=matrix_shape[1], input_length=max_sent_len, weights=[self.embedding.embedding_matrix], trainable=False)(words_input) if True: regularized_word_embeddings = EmbeddingLayer( input_dim=word_vocab_size, output_dim=matrix_shape[1], input_length=max_sent_len, weights=np.zeros(matrix_shape), trainable=False)(words_input) words_embedding = Add()([ un_trainable_words_embedding, regularized_word_embeddings ]) else: words_embedding = un_trainable_words_embedding word_rep = Concatenate()( [char_features, words_embedding, trainable_words_embedding]) else: word_rep = Concatenate()( [char_features, trainable_words_embedding]) x = Bidirectional(CuDNNLSTM(64, return_sequences=True))(word_rep) y = Bidirectional(CuDNNGRU(64, return_sequences=True))(x) atten_1 = Attention(max_sent_len)(x) # skip connect atten_2 = Attention(max_sent_len)(y) avg_pool = GlobalAveragePooling1D()(y) max_pool = GlobalMaxPooling1D()(y) conc = Concatenate()([atten_1, atten_2, avg_pool, max_pool]) conc = Dense(16, activation="relu")(conc) conc = Dropout(0.1)(conc) # x = Conv1D(filters=100, kernel_size=2)(x) # max_x = GlobalMaxPooling1D()(x) # avg_x = GlobalAveragePooling1D()(x) # x = Concatenate()([max_x, avg_x]) # x = Dense(16)(x) # x = Flatten()(char_sum) preds = Dense(1, activation='sigmoid')(conc) inputs = [chars_input, words_input, char_feats_input] self.model = Model(inputs=inputs, outputs=preds) self.model.compile(loss=self.loss, optimizer='adam', metrics=['accuracy', self.f1_score]) return self.model
def _create_model(self, input_shape): input_img = Input(shape=input_shape) # Create shared encoder. encoder_pipeline = [ Conv2D(64, (3, 3), activation='relu', padding='same'), Conv2D(128, (3, 3), activation='relu', padding='same'), MaxPooling2D((2, 2), padding='same', strides=2), Conv2D(128, (3, 3), activation='relu', padding='same'), MaxPooling2D((2, 2), padding='same', strides=2), Conv2D(64, (3, 3), activation='relu', padding='same'), Conv2D(8, (3, 3), activation='relu', padding='same'), ] # Split bag into single images to get encoded vectors splitted_imgs = SplitBagLayer(bag_size=input_shape[0])(input_img) encoded_img_matrices = [] for single_image in splitted_imgs: encoded_img = _attach_to_pipeline(single_image, encoder_pipeline) encoded_img_matrices.append(encoded_img) # We have v=(vec1, ... , vecN) where N is number of images in one bag # Now we need to do aggregation concat_matrix = concatenate( [Reshape((1, -1))(Flatten()(img)) for img in encoded_img_matrices], axis=1) # Now we have array with shape (num_vectors, latent_features). Let's aggregate them # NOTE: Aggregator is based on maximum # THIS IS THE PART WHERE WE LOOSE 1 DIMENSION (dimension of bags) aggregator = Lambda(lambda matrix: K.max(matrix, axis=1))( concat_matrix) # After encoding, we need to classify images classifier = Dense(128, activation=self.classifier_activation)(aggregator) classifier = Dropout(rate=0.5)(classifier) classifier = Dense(1, activation=self.classifier_activation, name='classifier_output')(classifier) decoder_pipeline = [ # TODO: maybe make activation functions tunable? Conv2D(128, (3, 3), activation='relu', padding='same'), UpSampling2D((2, 2)), Conv2D(64, (3, 3), activation='relu', padding='same'), UpSampling2D((2, 2)), Conv2D(32, (3, 3), activation='relu', padding='same'), Conv2D(input_shape[-1], (3, 3), activation='relu', padding='same'), # reshape (None, w, h, c) -> (None, 1, w, h, c) where 'w'=width, 'h'=height, 'c'=color_channel Reshape((1, *input_shape[1:])) ] decoded_images = [ _attach_to_pipeline(single_image, decoder_pipeline) for single_image in encoded_img_matrices ] decoded_images = concatenate(decoded_images, axis=1, name='decoded_output') model = Model(inputs=[input_img], outputs=[classifier, decoded_images]) model.compile(optimizer=self.optimizer, loss={ 'classifier_output': self.classifier_loss, 'decoded_output': self.decoder_loss }, loss_weights={ 'classifier_output': self.classifier_loss_weight, 'decoded_output': self.decoder_loss_weight }, metrics={'classifier_output': self.classifier_metrics}) return model
def Att_ConvLSTM(self, att_lstm_num, att_lstm_seq_len, lstm_seq_len, feature_vec_len, cnn_flat_size=128, lstm_out_size=128, nbhd_size=3, nbhd_type=2, output_shape=1, optimizer='adagrad', loss='mse', metrics=[]): flatten_att_nbhd_inputs = [ Input(shape=(nbhd_type, nbhd_size, nbhd_size), name="att_nbhd_volume_input_time_{0}_{1}".format( att + 1, ts + 1)) for ts in range(att_lstm_seq_len) for att in range(att_lstm_num) ] att_nbhd_inputs = [] for att in range(att_lstm_num): att_nbhd_inputs.append( flatten_att_nbhd_inputs[att * att_lstm_seq_len:(att + 1) * att_lstm_seq_len]) att_lstm_inputs = [ Input(shape=(att_lstm_seq_len, feature_vec_len), name="att_lstm_input_{0}".format(att + 1)) for att in range(att_lstm_num) ] lstm_inputs = Input(shape=(lstm_seq_len, feature_vec_len), name="lstm_input") nbhd_inputs = [ Input(shape=(nbhd_type, nbhd_size, nbhd_size), name="nbhd_volume_input_time_{0}".format(ts + 1)) for ts in range(lstm_seq_len) ] nbhd_convs = [ Conv2D(filters=64, kernel_size=(3, 3), padding="same", name="nbhd_convs_time0_{0}".format(ts + 1))(nbhd_inputs[ts]) for ts in range(lstm_seq_len) ] nbhd_convs = [ Activation("relu", name="nbhd_convs_activation_time0_{0}".format(ts + 1))( nbhd_convs[ts]) for ts in range(lstm_seq_len) ] nbhd_convs = [ Conv2D(filters=64, kernel_size=(3, 3), padding="same", name="nbhd_convs_time1_{0}".format(ts + 1))(nbhd_convs[ts]) for ts in range(lstm_seq_len) ] nbhd_convs = [ Activation("relu", name="nbhd_convs_activation_time1_{0}".format(ts + 1))( nbhd_convs[ts]) for ts in range(lstm_seq_len) ] nbhd_convs = [ Conv2D(filters=64, kernel_size=(3, 3), padding="same", name="nbhd_convs_time2_{0}".format(ts + 1))(nbhd_convs[ts]) for ts in range(lstm_seq_len) ] nbhd_convs = [ Activation("relu", name="nbhd_convs_activation_time2_{0}".format(ts + 1))( nbhd_convs[ts]) for ts in range(lstm_seq_len) ] nbhd_vecs = [ Flatten(name="nbhd_flatten_time_{0}".format(ts + 1))( nbhd_convs[ts]) for ts in range(lstm_seq_len) ] nbhd_vecs = [ Dense(units=cnn_flat_size, name="nbhd_dense_time_{0}".format(ts + 1))(nbhd_vecs[ts]) for ts in range(lstm_seq_len) ] nbhd_vecs = [ Activation("relu", name="nbhd_dense_activation_time_{0}".format(ts + 1))( nbhd_vecs[ts]) for ts in range(lstm_seq_len) ] nbhd_vec = Concatenate(axis=-1)(nbhd_vecs) nbhd_vec = Reshape(target_shape=(lstm_seq_len, cnn_flat_size))(nbhd_vec) lstm_input = Concatenate(axis=-1)([lstm_inputs, nbhd_vec]) lstm = LSTM(units=lstm_out_size, return_sequences=False, dropout=0.1, recurrent_dropout=0.1)(lstm_input) # attention part att_nbhd_convs = [[ Conv2D(filters=64, kernel_size=(3, 3), padding="same", name="att_nbhd_convs_time0_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_inputs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_convs = [[ Activation("relu", name="att_nbhd_convs_activation_time0_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_convs = [[ Conv2D(filters=64, kernel_size=(3, 3), padding="same", name="att_nbhd_convs_time1_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_convs = [[ Activation("relu", name="att_nbhd_convs_activation_time1_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_convs = [[ Conv2D(filters=64, kernel_size=(3, 3), padding="same", name="att_nbhd_convs_time2_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_convs = [[ Activation("relu", name="att_nbhd_convs_activation_time2_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_vecs = [[ Flatten( name="att_nbhd_flatten_time_{0}_{1}".format(att + 1, ts + 1))( att_nbhd_convs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_vecs = [[ Dense(units=cnn_flat_size, name="att_nbhd_dense_time_{0}_{1}".format(att + 1, ts + 1))( att_nbhd_vecs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_vecs = [[ Activation("relu", name="att_nbhd_dense_activation_time_{0}_{1}".format( att + 1, ts + 1))(att_nbhd_vecs[att][ts]) for ts in range(att_lstm_seq_len) ] for att in range(att_lstm_num)] att_nbhd_vec = [ Concatenate(axis=-1)(att_nbhd_vecs[att]) for att in range(att_lstm_num) ] att_nbhd_vec = [ Reshape(target_shape=(att_lstm_seq_len, cnn_flat_size))(att_nbhd_vec[att]) for att in range(att_lstm_num) ] att_lstm_input = [ Concatenate(axis=-1)([att_lstm_inputs[att], att_nbhd_vec[att]]) for att in range(att_lstm_num) ] att_lstms = [ LSTM(units=lstm_out_size, return_sequences=True, dropout=0.1, recurrent_dropout=0.1, name="att_lstm_{0}".format(att + 1))(att_lstm_input[att]) for att in range(att_lstm_num) ] # compare att_low_level = [ attention.Attention(method='cba')([att_lstms[att], lstm]) for att in range(att_lstm_num) ] att_low_level = Concatenate(axis=-1)(att_low_level) att_low_level = Reshape(target_shape=(att_lstm_num, lstm_out_size))(att_low_level) att_high_level = LSTM(units=lstm_out_size, return_sequences=True, dropout=0.1, recurrent_dropout=0.1)(att_low_level) att_high_level = attention.Attention(method='cba')( [att_high_level, lstm]) att_high_level = Reshape(target_shape=(att_lstm_num, lstm_out_size))(att_low_level) lstm_all = Concatenate(axis=-1)([att_high_level, lstm]) lstm_all = Dense(units=output_shape)(lstm_all) pred_volume = Activation('tanh')(lstm_all) # inputs=[] # inputs.append(flatten_att_nbhd_inputs) # inputs.append(att_lstm_inputs) # inputs.append(nbhd_inputs) # inputs.append(lstm_inputs) inputs = flatten_att_nbhd_inputs + att_lstm_inputs + nbhd_inputs + [ lstm_inputs, ] model = Model(inputs=inputs, outputs=pred_volume) model.compile(optimizer=optimizer, loss=loss, metrics=metrics) model.summary() return model
from keras import Input, layers, Model input_tensor = Input(shape=(64,)) x = layers.Dense(32, activation='relu')(input_tensor) x = layers.Dense(32, activation='relu')(x) output_tensor = layers.Dense(10,activation='softmax')(x) model = Model(input_tensor,output_tensor)
validation_generator = validation_datagen.flow_from_directory( input_path + 'validation', shuffle=False, class_mode='binary', target_size=(224, 224)) conv_base = ResNet50(include_top=False, weights='imagenet') for layer in conv_base.layers: layer.trainable = False x = conv_base.output x = layers.GlobalAveragePooling2D()(x) x = layers.Dense(128, activation='relu')(x) predictions = layers.Dense(2, activation='softmax')(x) model = Model(conv_base.input, predictions) optimizer = keras.optimizers.Adam() model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) history = model.fit_generator(generator=train_generator, epochs=3, validation_data=validation_generator, steps_per_epoch=100 // 12, validation_steps=10) # architecture and weights to HDF5 model.save('models/keras/model.h5')
train_x = train_x[:, :, :, 58:186] train_y = train_y[:, :, :, 4:132] enter_shape = train_x.shape + (1, ) train_x = train_x.reshape(*enter_shape) train_y = train_y.reshape(*enter_shape) # Visualisation # train_y = np.array(train_y[0]) # print(train_x.shape) # mlab.contour3d(train_y) # mlab.savefig('surface.obj') # input() # input_layer = Input(enter_shape[1:]) output_layer = build_model(input_layer, start_neurons) model = Model(input_layer, output_layer) model.compile(loss=bce_jaccard_loss, optimizer=Adam(lr=1e-3), metrics=dice_coef) model.save_weights('./keras.weights') history = model.fit(train_x, train_y, batch_size=1, epochs=1, verbose=1, validation_split=1) print(model.predict(train_x).shape)
if __name__ == "__main__": valuesegments, threshold = find_value_segments(item_orig2, prototype2, timesteps2, 4, 2, ranking) valuesegments2 = [] for segment in valuesegments: if segment.signal in selection: print("Attention for signal {}".format(segment.signal)) valuesegments2.append(segment) visualise_difference_segments(item_orig2, prototype2, timestep, 256, valuesegments2, signalnames2, 4, ranking) int_model = Model(inputs=model.inputs, outputs=[model.layers[-2].output]) for i, layer in enumerate(int_model.layers): layer._name = 'layer_' + str(i) item_scores = int_model.predict(item_orig.reshape(1, timesteps2, nsignals))[0] item_ranking = np.argsort(-item_scores) relevant_nodes_item = item_ranking[:4] print(relevant_nodes_item) prototype_scores = int_model.predict( prototype.reshape(1, timesteps2, nsignals))[0] prototype_ranking = np.argsort(-prototype_scores) relevant_nodes_prototype = prototype_ranking[:4]
def getActivationLayers(model): intermediate_layer_model_1 = Model(inputs=model.input, outputs=model.get_layer("activation_7").output) intermediate_layer_model_2 = Model(inputs=model.input, outputs=model.get_layer("activation_8").output) #intermediate_layer_model_3 = Model(inputs=model.input, outputs=model.get_layer("activation_3").output) return intermediate_layer_model_1, intermediate_layer_model_2
def score_and_write_batch(model: keras.Model, file_out: TextIO, batch_size: int, python_batch_size: int, tensor_type: str, annotation_set: str, window_size: int, read_limit: int, tensor_dir: str = '') -> None: """Score a batch of variants with a CNN model. Write tab delimited temp file with scores. This function is tightly coupled with the CNNScoreVariants.java It requires data written to the fifo in the order given by transferToPythonViaFifo Arguments model: a keras model file_out: The temporary VCF-like file where variants scores will be written batch_size: The total number of variants available in the fifo python_batch_size: the number of variants to process in each inference tensor_type: The name for the type of tensor to make annotation_set: The name for the set of annotations to use window_size: The size of the context window of genomic bases, i.e the width of the tensor read_limit: The maximum number of reads to encode in a tensor, i.e. the height of the tensor tensor_dir : If this path exists write hd5 files for each tensor (optional for debugging) """ annotation_batch = [] reference_batch = [] variant_types = [] variant_data = [] read_batch = [] for _ in range(batch_size): fifo_line = tool.readDataFIFO() fifo_data = fifo_line.split(defines.DATA_TYPE_SEPARATOR) variant_data.append(fifo_data[CONTIG_FIFO_INDEX] + defines.DATA_TYPE_SEPARATOR + fifo_data[POS_FIFO_INDEX] + defines.DATA_TYPE_SEPARATOR + fifo_data[REF_FIFO_INDEX] + defines.DATA_TYPE_SEPARATOR + fifo_data[ALT_FIFO_INDEX]) reference_batch.append( reference_string_to_tensor(fifo_data[REF_STRING_FIFO_INDEX])) annotation_batch.append( annotation_string_to_tensor(annotation_set, fifo_data[ANNOTATION_FIFO_INDEX])) variant_types.append(fifo_data[VARIANT_TYPE_FIFO_INDEX].strip()) fifo_idx = VARIANT_FIFO_FIELDS if tensor_type in defines.TENSOR_MAPS_2D and len(fifo_data) > fifo_idx: read_tuples = [] var = Variant(fifo_data[CONTIG_FIFO_INDEX], int(fifo_data[POS_FIFO_INDEX]), fifo_data[POS_FIFO_INDEX], fifo_data[ALT_FIFO_INDEX], fifo_data[VARIANT_TYPE_FIFO_INDEX]) while fifo_idx + READ_ELEMENTS <= len(fifo_data): read_tuples.append( Read( fifo_data[fifo_idx + READ_BASES_FIFO_INDEX], list( map( int, fifo_data[fifo_idx + READ_QUAL_FIFO_INDEX].split( defines.DATA_VALUE_SEPARATOR))), fifo_data[fifo_idx + READ_CIGAR_FIFO_INDEX], bool_from_java(fifo_data[fifo_idx + READ_REVERSE_FIFO_INDEX]), bool_from_java( fifo_data[fifo_idx + READ_MATE_REVERSE_FIFO_INDEX]), bool_from_java( fifo_data[fifo_idx + READ_FIRST_IN_PAIR_FIFO_INDEX]), int(fifo_data[fifo_idx + READ_MQ_FIFO_INDEX]), int(fifo_data[fifo_idx + READ_REF_START_FIFO_INDEX]))) fifo_idx += READ_ELEMENTS _, ref_start, _ = get_variant_window(window_size, var) insert_dict = get_inserts(read_tuples, var, window_size) tensor = read_tuples_to_tensor(read_tuples, ref_start, insert_dict, tensor_type, window_size, read_limit) reference_sequence_into_tensor(fifo_data[4], tensor, insert_dict, window_size, read_limit) if os.path.exists(tensor_dir): _write_tensor_to_hd5(tensor, annotation_batch[-1], fifo_data[0], fifo_data[1], fifo_data[6], tensor_type, annotation_set, tensor_dir) read_batch.append(tensor) if tensor_type in defines.TENSOR_MAPS_1D: predictions = model.predict( [np.array(reference_batch), np.array(annotation_batch)], batch_size=python_batch_size) elif tensor_type in defines.TENSOR_MAPS_2D: predictions = model.predict( [np.array(read_batch), np.array(annotation_batch)], batch_size=python_batch_size) else: raise ValueError('Unknown tensor mapping. Check architecture file.', tensor_type) indel_scores = predictions_to_indel_scores(predictions) snp_scores = predictions_to_snp_scores(predictions) for i in range(batch_size): if 'SNP' == variant_types[i]: file_out.write(variant_data[i] + defines.DATA_TYPE_SEPARATOR + '{0:.3f}'.format(snp_scores[i]) + '\n') elif 'INDEL' == variant_types[i]: file_out.write(variant_data[i] + defines.DATA_TYPE_SEPARATOR + '{0:.3f}'.format(indel_scores[i]) + '\n') else: file_out.write( variant_data[i] + defines.DATA_TYPE_SEPARATOR + '{0:.3f}'.format(max(snp_scores[i], indel_scores[i])) + '\n')
def VAE(original_dim, hidden_dim=128, encoder_dim=16): # encoder inputs = Input(shape=(original_dim, )) hidden_layer1 = Dense(hidden_dim, activation='relu')(inputs) encoded = Dense(encoder_dim, activation='relu')(hidden_layer1) z_mean = Dense(encoder_dim)(encoded) z_log_var = Dense(encoder_dim)(encoded) #使用均值变量(mean vector)和标准差变量(standard deviation vector)合成隐变量 def sampling(args): z_mean, z_log_var = args #使用标准正态分布初始化 epsilon = K.random_normal(shape=(K.shape(z_mean)[0], hidden_dim), mean=0., stddev=1.0) #合成公式 return z_mean + K.exp(z_log_var / 2) * epsilon z = Lambda(sampling, output_shape=(hidden_dim, ))([z_mean, z_log_var]) encoder = Model(inputs=inputs, outputs=z) encoder.summary() # decoder hidden_layer2 = Dense(hidden_dim, activation='relu')(z) decoded = Dense(original_dim, activation='tanh')(hidden_layer2) decoder = Model(inputs=encoded, outputs=decoded) decoder.summary() # vae vae = Model(inputs, decoded) reconstruction_loss = original_dim * binary_crossentropy(inputs, decoded) kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = -0.5 * K.sum(kl_loss, axis=-1) vae_loss = K.mean(reconstruction_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer='adam') vae.summary() plot_model(vae, to_file='vae_mlp.png', show_shapes=True)
rd.shuffle(X_Y) X, Y = zip(*X_Y) oH_x_train = np.array(X) oH_y_train = np.array(Y) #Change channel from last to first set_image_data_format('channels_first') ### Model inputs = Input(shape=(14, 32, 32, 32)) conv1 = Conv3D(4, (2,2,2), padding="valid", activation="relu")(inputs) conv2 = Conv3D(16, (2,2,2), padding="valid", activation="relu")(conv1) drop1 = Dropout(0.2)(conv2) pool1 = MaxPooling3D(pool_size=(2,2,2))(drop1) drop2 = Dropout(0.2)(pool1) flat1 = Flatten()(drop2) outputs = Dense(4, activation = "softmax")(flat1) model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) earlyStop = callbacks.EarlyStopping(monitor="val_loss", patience=10, mode="min") best_model_file = ('best_model.h5') best_model = callbacks.ModelCheckpoint(best_model_file, monitor='val_loss', save_best_only='TRUE') my_model = model.fit(oH_x_train, oH_y_train, batch_size=16, epochs=300, validation_split = 0.3, callbacks=[earlyStop,best_model])
vocabulary_size = 50000 num_income_groups = 10 posts_input = Input(shape=(None,), dtype='int32', name='posts') embedded_posts = layers.Embedding(vocabulary_size, 256)(posts_input) x = layers.Conv1D(128, 5, activation='relu')(embedded_posts) x = layers.MaxPooling1D(5)(x) x = layers.Conv1D(256, 5, activation='relu')(x) x = layers.Conv1D(256, 5, activation='relu')(x) x = layers.MaxPooling1D(5)(x) x = layers.Conv1D(256, 5, activation='relu')(x) x = layers.Conv1D(256, 5, activation='relu')(x) x = layers.GlobalMaxPooling1D()(x) x = layers.Dense(128, activation='relu')(x) age_prediction = layers.Dense(1, name='age')(x) income_prediction = layers.Dense( num_income_groups, activation='softmax', name='income')(x) gender_prediction = layers.Dense(1, activation='sigmoid', name='gender')(x) model = Model(posts_input, [age_prediction, income_prediction, gender_prediction]) model.compile(optimizer='rmsprop', loss={'age': 'mse', 'income': 'categorical_crossentropy', 'gender': 'binary_crossentropy'}, loss_weights={'age': 0.25, 'income': 1.0, 'gender': 10.0})
class CharacterTagger: """A class for character-based neural morphological tagger Parameters: symbols: character vocabulary tags: morphological tags vocabulary word_rnn: the type of character-level network (only `cnn` implemented) char_embeddings_size: the size of character embeddings char_conv_layers: the number of convolutional layers on character level char_window_size: the width of convolutional filter (filters). It can be a list if several parallel filters are applied, for example, [2, 3, 4, 5]. char_filters: the number of convolutional filters for each window width. It can be a number, a list (when there are several windows of different width on a single convolution layer), a list of lists, if there are more than 1 convolution layers, or **None**. If **None**, a layer with width **width** contains min(**char_filter_multiple** * **width**, 200) filters. char_filter_multiple: the ratio between filters number and window width char_highway_layers: the number of highway layers on character level conv_dropout: the ratio of dropout between convolutional layers highway_dropout: the ratio of dropout between highway layers, intermediate_dropout: the ratio of dropout between convolutional and highway layers on character level lstm_dropout: dropout ratio in word-level LSTM word_vectorizers: list of parameters for additional word-level vectorizers, for each vectorizer it stores a pair of vectorizer dimension and the dimension of the corresponding word embedding word_lstm_layers: the number of word-level LSTM layers word_lstm_units: hidden dimensions of word-level LSTMs word_dropout: the ratio of dropout before word level (it is applied to word embeddings) regularizer: l2 regularization parameter verbose: the level of verbosity """ def __init__(self, symbols: DefaultVocabulary, tags: DefaultVocabulary, word_rnn: str = "cnn", char_embeddings_size: int = 16, char_conv_layers: int = 1, char_window_size: Union[int, List[int]] = 5, char_filters: Union[int, List[int]] = None, char_filter_multiple: int = 25, char_highway_layers: int = 1, conv_dropout: float = 0.0, highway_dropout: float = 0.0, intermediate_dropout: float = 0.0, lstm_dropout: float = 0.0, word_vectorizers: List[Tuple[int, int]] = None, word_lstm_layers: int = 1, word_lstm_units: Union[int, List[int]] = 128, word_dropout: float = 0.0, regularizer: float = None, verbose: int = 1): self.symbols = symbols self.tags = tags self.word_rnn = word_rnn self.char_embeddings_size = char_embeddings_size self.char_conv_layers = char_conv_layers self.char_window_size = char_window_size self.char_filters = char_filters self.char_filter_multiple = char_filter_multiple self.char_highway_layers = char_highway_layers self.conv_dropout = conv_dropout self.highway_dropout = highway_dropout self.intermediate_dropout = intermediate_dropout self.lstm_dropout = lstm_dropout self.word_dropout = word_dropout self.word_vectorizers = word_vectorizers # a list of additional vectorizer dimensions self.word_lstm_layers = word_lstm_layers self.word_lstm_units = word_lstm_units self.regularizer = regularizer self.verbose = verbose self._initialize() self.build() def _initialize(self): if isinstance(self.char_window_size, int): self.char_window_size = [self.char_window_size] if self.char_filters is None or isinstance(self.char_filters, int): self.char_filters = [self.char_filters] * len(self.char_window_size) if len(self.char_window_size) != len(self.char_filters): raise ValueError("There should be the same number of window sizes and filter sizes") if isinstance(self.word_lstm_units, int): self.word_lstm_units = [self.word_lstm_units] * self.word_lstm_layers if len(self.word_lstm_units) != self.word_lstm_layers: raise ValueError("There should be the same number of lstm layer units and lstm layers") if self.word_vectorizers is None: self.word_vectorizers = [] if self.regularizer is not None: self.regularizer = kreg.l2(self.regularizer) if self.verbose > 0: log.info("{} symbols, {} tags in CharacterTagger".format(self.symbols_number_, self.tags_number_)) @property def symbols_number_(self) -> int: """Character vocabulary size """ return len(self.symbols) @property def tags_number_(self) -> int: """Tag vocabulary size """ return len(self.tags) def build(self): """Builds the network using Keras. """ word_inputs = kl.Input(shape=(None, MAX_WORD_LENGTH+2), dtype="int32") inputs = [word_inputs] word_outputs = self._build_word_cnn(word_inputs) if len(self.word_vectorizers) > 0: additional_word_inputs = [kl.Input(shape=(None, input_dim), dtype="float32") for input_dim, dense_dim in self.word_vectorizers] inputs.extend(additional_word_inputs) additional_word_embeddings = [kl.Dense(dense_dim)(additional_word_inputs[i]) for i, (_, dense_dim) in enumerate(self.word_vectorizers)] word_outputs = kl.Concatenate()([word_outputs] + additional_word_embeddings) outputs, lstm_outputs = self._build_basic_network(word_outputs) compile_args = {"optimizer": ko.nadam(lr=0.002, clipnorm=5.0), "loss": "categorical_crossentropy", "metrics": ["accuracy"]} self.model_ = Model(inputs, outputs) self.model_.compile(**compile_args) if self.verbose > 0: self.model_.summary(print_fn=log.info) return self def _build_word_cnn(self, inputs): """Builds word-level network """ inputs = kl.Lambda(kb.one_hot, arguments={"num_classes": self.symbols_number_}, output_shape=lambda x: tuple(x) + (self.symbols_number_,))(inputs) char_embeddings = kl.Dense(self.char_embeddings_size, use_bias=False)(inputs) conv_outputs = [] self.char_output_dim_ = 0 for window_size, filters_number in zip(self.char_window_size, self.char_filters): curr_output = char_embeddings curr_filters_number = (min(self.char_filter_multiple * window_size, 200) if filters_number is None else filters_number) for _ in range(self.char_conv_layers - 1): curr_output = kl.Conv2D(curr_filters_number, (1, window_size), padding="same", activation="relu", data_format="channels_last")(curr_output) if self.conv_dropout > 0.0: curr_output = kl.Dropout(self.conv_dropout)(curr_output) curr_output = kl.Conv2D(curr_filters_number, (1, window_size), padding="same", activation="relu", data_format="channels_last")(curr_output) conv_outputs.append(curr_output) self.char_output_dim_ += curr_filters_number if len(conv_outputs) > 1: conv_output = kl.Concatenate(axis=-1)(conv_outputs) else: conv_output = conv_outputs[0] highway_input = kl.Lambda(kb.max, arguments={"axis": -2})(conv_output) if self.intermediate_dropout > 0.0: highway_input = kl.Dropout(self.intermediate_dropout)(highway_input) for i in range(self.char_highway_layers - 1): highway_input = Highway(activation="relu")(highway_input) if self.highway_dropout > 0.0: highway_input = kl.Dropout(self.highway_dropout)(highway_input) highway_output = Highway(activation="relu")(highway_input) return highway_output def _build_basic_network(self, word_outputs): """ Creates the basic network architecture, transforming word embeddings to intermediate outputs """ if self.word_dropout > 0.0: lstm_outputs = kl.Dropout(self.word_dropout)(word_outputs) else: lstm_outputs = word_outputs for j in range(self.word_lstm_layers-1): lstm_outputs = kl.Bidirectional( kl.LSTM(self.word_lstm_units[j], return_sequences=True, dropout=self.lstm_dropout))(lstm_outputs) lstm_outputs = kl.Bidirectional( kl.LSTM(self.word_lstm_units[-1], return_sequences=True, dropout=self.lstm_dropout))(lstm_outputs) pre_outputs = kl.TimeDistributed( kl.Dense(self.tags_number_, activation="softmax", activity_regularizer=self.regularizer), name="p")(lstm_outputs) return pre_outputs, lstm_outputs def _transform_batch(self, data, labels=None, transform_to_one_hot=True): data, additional_data = data[0], data[1:] L = max(len(x) for x in data) X = np.array([self._make_sent_vector(x, L) for x in data]) X = [X] + [np.array(x) for x in additional_data] if labels is not None: Y = np.array([self._make_tags_vector(y, L) for y in labels]) if transform_to_one_hot: Y = to_one_hot(Y, len(self.tags)) return X, Y else: return X def train_on_batch(self, data: List[Iterable], labels: Iterable[list]) -> None: """Trains model on a single batch Args: data: a batch of word sequences labels: a batch of correct tag sequences Returns: the trained model """ X, Y = self._transform_batch(data, labels) self.model_.train_on_batch(X, Y) def predict_on_batch(self, data: Union[list, tuple], return_indexes: bool = False) -> List[List[str]]: """ Makes predictions on a single batch Args: data: a batch of word sequences together with additional inputs return_indexes: whether to return tag indexes in vocabulary or tags themselves Returns: a batch of label sequences """ X = self._transform_batch(data) objects_number, lengths = len(X[0]), [len(elem) for elem in data[0]] Y = self.model_.predict_on_batch(X) labels = np.argmax(Y, axis=-1) answer: List[List[str]] = [None] * objects_number for i, (elem, length) in enumerate(zip(labels, lengths)): elem = elem[:length] answer[i] = elem if return_indexes else self.tags.idxs2toks(elem) return answer def _make_sent_vector(self, sent: List, bucket_length: int =None) -> np.ndarray: """Transforms a sentence to Numpy array, which will be the network input. Args: sent: input sentence bucket_length: the width of the bucket Returns: A 3d array, answer[i][j][k] contains the index of k-th letter in j-th word of i-th input sentence. """ bucket_length = bucket_length or len(sent) answer = np.zeros(shape=(bucket_length, MAX_WORD_LENGTH+2), dtype=np.int32) for i, word in enumerate(sent): answer[i, 0] = self.tags.tok2idx("BEGIN") m = min(len(word), MAX_WORD_LENGTH) for j, x in enumerate(word[-m:]): answer[i, j+1] = self.symbols.tok2idx(x) answer[i, m+1] = self.tags.tok2idx("END") answer[i, m+2:] = self.tags.tok2idx("PAD") return answer def _make_tags_vector(self, tags, bucket_length=None) -> np.ndarray: """Transforms a sentence of tags to Numpy array, which will be the network target. Args: tags: input sentence of tags bucket_length: the width of the bucket Returns: A 2d array, answer[i][j] contains the index of j-th tag in i-th input sentence. """ bucket_length = bucket_length or len(tags) answer = np.zeros(shape=(bucket_length,), dtype=np.int32) for i, tag in enumerate(tags): answer[i] = self.tags.tok2idx(tag) return answer def save(self, outfile) -> None: """Saves model weights to a file Args: outfile: file with model weights (other model components should be given in config) """ self.model_.save_weights(outfile) def load(self, infile) -> None: """Loads model weights from a file Args: infile: file to load model weights from """ self.model_.load_weights(infile)
#sequenctial models using functional API from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Flatten from keras import Model #input sample size of 2 , 1 output perceptron input = Input(shape=(2,)) output = Dense(1)(input) model1 = Model(inputs=input, outputs=output) print(model1.summary()) input = Input(shape=(2,)) tmp = Dense(1) output = tmp(input) model1 = Model(inputs=input, outputs=output) print(model1.summary()) #input sample size of 10, 3 hidden layers with 10,20,10 perceptrons, output layer with 1 peceptron input = Input(shape=(10,)) hidden1 = Dense(10, activation='relu')(input) hidden2 = Dense(20, activation='relu')(hidden1) hidden3 = Dense(10, activation='relu')(hidden2) output = Dense(1, activation='sigmoid')(hidden3) model2 = Model(inputs=input, outputs=output) print(model2.summary()) #input image of size 64*64*1, 2 conv-pool layers of 32 and 16, 1 hidden layer of size 10, output layer of size 2 input = Input(shape=(64,64,1)) conv1 = Conv2D(32, kernel_size=4, activation='relu')(input) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
def main(): # fix random seed for reproducibility numpy.random.seed(7) # Get CLI arguments parser = argparse.ArgumentParser() parser.add_argument( '--gpus', help='Number of GPUs to use.', type=int, default=1) args = parser.parse_args() gpus = args.gpus # load the dataset dataframe = DataFrame( [0.00000, 5.99000, 11.92016, 17.73121, 23.36510, 28.76553, 33.87855, 38.65306, 43.04137, 46.99961, 50.48826, 53.47244, 55.92235, 57.81349, 59.12698, 59.84970, 59.97442, 59.49989, 58.43086, 56.77801, 54.55785, 51.79256, 48.50978, 44.74231, 40.52779, 35.90833, 30.93008, 25.64279, 20.09929, 14.35496, 8.46720, 2.49484, -3.50245, -9.46474, -15.33247, -21.04699, -26.55123, -31.79017, -36.71147, -41.26597, -45.40815, -49.09663, -52.29455, -54.96996, -57.09612, -58.65181, -59.62146, -59.99540, -59.76988, -58.94716, -57.53546, -55.54888, -53.00728, -49.93605, -46.36587, -42.33242, -37.87600, -33.04113, -27.87613, -22.43260, -16.76493, -10.92975, -4.98536, 1.00883, 6.99295, 12.90720, 18.69248, 24.29100, 29.64680, 34.70639, 39.41920, 43.73814, 47.62007, 51.02620, 53.92249, 56.28000, 58.07518, 59.29009, 59.91260, 59.93648, 59.36149, 58.19339, 56.44383, 54.13031, 51.27593, 47.90923, 44.06383, 39.77815, 35.09503, 30.06125, 24.72711, 19.14590, 13.37339, 7.46727, 1.48653, -4.50907, -10.45961, -16.30564, -21.98875, -27.45215, -32.64127, -37.50424, -41.99248, -46.06115, -49.66959, -52.78175, -55.36653, -57.39810, -58.85617, -59.72618, -59.99941, -59.67316, -58.75066, -57.24115, -55.15971, -52.52713, -49.36972, -45.71902, -41.61151, -37.08823, -32.19438, -26.97885, -21.49376, -15.79391, -9.93625, -3.97931, 2.01738, 7.99392, 13.89059, 19.64847, 25.21002, 30.51969, 35.52441, 40.17419, 44.42255, 48.22707, 51.54971, 54.35728, 56.62174, 58.32045, 59.43644, 59.95856, 59.88160, 59.20632, 57.93947, 56.09370, 53.68747, 50.74481, 47.29512, 43.37288, 39.01727, 34.27181, 29.18392, 23.80443, 18.18710, 12.38805, 6.46522, 0.47779, -5.51441, -11.45151]) dataset = dataframe.values dataset = dataset.astype('float32') # normalize the dataset scaler = MinMaxScaler(feature_range=(0, 1)) dataset = scaler.fit_transform(dataset) # split into train and test sets train_size = int(len(dataset) * 0.67) test_size = len(dataset) - train_size train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :] # reshape into X=t and Y=t+1 look_back = 1 trainX, trainY = create_dataset(train, look_back) testX, testY = create_dataset(test, look_back) # reshape input to be [samples, time steps, features] trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1])) testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1])) # Create layers for model x_tensor = Input(shape=(1, look_back)) layer_1 = LSTM(4)(x_tensor) y_tensor = Dense(1)(layer_1) # Create and fit the LSTM network with tf.device('/cpu:0'): serial_model = Model(inputs=x_tensor, outputs=y_tensor) # Modify model for GPUs if necessary if gpus == 1: parallel_model = serial_model else: parallel_model = multi_gpu_model( serial_model, cpu_relocation=True, gpus=gpus) parallel_model.compile( loss='mean_squared_error', optimizer='adam') parallel_model.fit( trainX, trainY, epochs=100, batch_size=int(dataset.size * gpus / 20), verbose=2) # make predictions if gpus == 1: trainPredict = parallel_model.predict(trainX) testPredict = parallel_model.predict(testX) else: trainPredict = serial_model.predict(trainX) testPredict = serial_model.predict(testX) # invert predictions trainPredict = scaler.inverse_transform(trainPredict) trainY = scaler.inverse_transform([trainY]) testPredict = scaler.inverse_transform(testPredict) testY = scaler.inverse_transform([testY]) # calculate root mean squared error trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:, 0])) print('Train Score: %.2f RMSE' % (trainScore)) testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:, 0])) print('Test Score: %.2f RMSE' % (testScore)) # shift train predictions for plotting trainPredictPlot = numpy.empty_like(dataset) trainPredictPlot[:, :] = numpy.nan trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict # shift test predictions for plotting testPredictPlot = numpy.empty_like(dataset) testPredictPlot[:, :] = numpy.nan testPredictPlot[ len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict # plot baseline and predictions plt.plot(scaler.inverse_transform(dataset), label='Complete Data') plt.plot(trainPredictPlot, label='Training Data') plt.plot(testPredictPlot, label='Prediction Data') plt.legend(loc='upper left') plt.title('Using {} GPUs'.format(gpus)) plt.show()
class PolicyValueNetwork: """ AlphaZero Residual-CNN """ def __init__(self, model_file=None): # Build Network Architecture input_shape = Board().encoded_states().shape # (6, 15, 15) inputs = Input(input_shape) shared_net = Sequential([ *ConvBlock(32, input_shape=input_shape), *ConvBlock(64), *ConvBlock(128) ], "shared_net") policy_head = Sequential([ shared_net, *ConvBlock(4, (1, 1), "relu"), Flatten(), Dense(Game["board_size"], kernel_regularizer=l2()), Activation("softmax") ], "policy_head") value_head = Sequential([ shared_net, *ConvBlock(2, (1, 1), "relu"), Flatten(), Dense(64, activation="relu", kernel_regularizer=l2()), Dense(1, kernel_regularizer=l2()), Activation("tanh") ], "value_head") self.model = Model( inputs, [value_head(inputs), policy_head(inputs)] ) if model_file is not None: self.restore_model(model_file) def compile(self, opt): """ Optimization and Loss definition """ self.model.compile( optimizer=sgd(), loss=["mse", "categorical_crossentropy"] ) def eval_state(self, state): """ Evaluate a board state. """ vp = self.model.predict_on_batch(state.encoded_states()[np.newaxis, :]) # format to (float, np.array((255,1),dtype=float)) structure return vp[0][0][0], vp[1][0] def train_step(self, optimizer): """ One Network Tranning step. """ opt = self.model.optimizer K.set_value(opt.lr, optimizer["lr"]) K.set_value(opt.momentum, optimizer["momentum"]) # loss = self.model.train_on_batch(inputs, [winner, probs]) # return loss def save_model(self, filename): base_path = "{}/keras".format(TRAINING_CONFIG["model_path"]) if not os.path.exists(base_path): os.mkdir(base_path) self.model.save_weights("{}/{}.h5".format(base_path, filename)) def restore_model(self, filename): base_path = "{}/keras".format(TRAINING_CONFIG["model_path"]) if os.path.exists("{}/{}.h5".format(base_path, filename)): self.model.load_weights("{}/{}.h5".format(base_path, filename))
in_channels=112, out_channels=160, kernel_size=(5, 5), strides=(2, 2)) x = MBConv_SE(inputs=x, expansion=6, in_channels=160, out_channels=160, kernel_size=(5, 5), strides=(1, 1)) x = MBConv_SE(inputs=x, expansion=6, in_channels=160, out_channels=160, kernel_size=(5, 5), strides=(1, 1)) # MBConv6 (k3x3) x1 x = MBConv(inputs=x, expansion=6, in_channels=160, out_channels=320, kernel_size=(3, 3), strides=(1, 1)) x = GlobalAveragePooling2D()(x) output_tensor = Dense(6, activation='softmax')(x) model = Model(input_tensor, output_tensor) model.summary()
answer_vocabulary_size = 500 text_input = Input(shape=(None,), dtype='int32', name='text') embedded_text = layers.Embedding(text_vocabulary_size, 64)(text_input) encoded_text = layers.LSTM(32)(embedded_text) question_input = Input(shape=(None,), dtype='int32', name='question') embedded_question = layers.Embedding( question_vocabulary_size, 32)(question_input) encoded_question = layers.LSTM(16)(embedded_question) concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1) answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated) model = Model([text_input, question_input], answer) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc']) # %% num_samples = 1000 max_length = 100 text = np.random.randint(1, text_vocabulary_size, size=(num_samples, max_length)) question = np.random.randint( 1, question_vocabulary_size, size=(num_samples, max_length)) answers = np.random.randint(1, answer_vocabulary_size, size=(num_samples,)) answers = keras.utils.to_categorical(answers, answer_vocabulary_size)
from keras.layers import Input, Dense, Conv2D, MaxPooling2D from keras import Model from keras.layers import concatenate input_img = Input(shape=(256, 256, 3)) tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_1 = Conv2D(64, (3, 3), padding='same', activation='relu')(tower_1) tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input_img) tower_2 = Conv2D(64, (5, 5), padding='same', activation='relu')(tower_2) tower_3 = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(input_img) tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3) output = concatenate([tower_1, tower_2, tower_3], axis=1) model4 = Model(inputs=input_img, outputs=output) print(model4.summary())
#数据准备,将28*28矩阵转换成1*784,方便BP神经网络输入层784个神经元读取 x_train = x_train.reshape(len(x_train), np.prod(x_train.shape[1:])) x_test = x_test.reshape(len(x_test), np.prod(x_test.shape[1:])) #------------------------------------------构建多层自编码器模型---------------------- input_size = 784 hidden_size = 128 code_size = 64 #定义神经网络层数 x = Input(shape=(input_size,)) hidden_1 = Dense(hidden_size, activation='relu')(x) h = Dense(code_size, activation='relu')(hidden_1) hidden_2 = Dense(hidden_size, activation='relu')(h) r = Dense(input_size, activation='sigmoid')(hidden_2) autoencoder = Model(inputs=x, outputs=r) autoencoder.compile(optimizer='adam', loss='mse') #---------------------------------------------模型训练与可视化------------------------------ epochs = 5 batch_size = 128 history = autoencoder.fit(x_train, x_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, x_test)) #------------------------------------------查看自编码器的压缩效果----------------------------- #为隐藏层的结果(encoder的最后一层),只取编码器做模型 conv_encoder = Model(x, h) encoded_imgs = autoencoder.predict(x_test)
def __build_network(self, nr_of_boards: int = 1, num_residual_blocks: int = 1) -> Model: # 2D input data in_en_passants_2d = Input(shape=(nr_of_boards, 8, 8), name="in_en_passants_2d") in_piece_positions_2d = Input(shape=(nr_of_boards, 12, 8, 8), name="in_piece_positions_2d") in_promotion_map_2d = Input(shape=(nr_of_boards, 8, 8), name="in_promotion_map_2d") # Scalar input data in_pockets = Input(shape=(nr_of_boards, 2, 5), name="in_pockets") in_pockets_normalized = Lambda(lambda x: x / 16.0)(in_pockets) in_turns = Input(shape=(nr_of_boards, 2), name="in_turn") in_clocks = Input(shape=(nr_of_boards, 2), name="in_clocks") in_clocks_normalized = Lambda(lambda x: x / 5.0)(in_clocks) in_castling = Input(shape=(nr_of_boards, 2, 2), name="in_castling") # Reshape scalar data pockets_reshaped = Reshape( (5 * 2 * nr_of_boards, ), name="pockets_reshaped")(in_pockets_normalized) turns_reshaped = Reshape((2 * nr_of_boards, ), name="turns_reshaped")(in_turns) castling_reshaped = Reshape((2 * 2 * nr_of_boards, ), name="castling_reshaped")(in_castling) clock_reshaped = Reshape((2 * nr_of_boards, ), name="clock_reshaped")(in_clocks_normalized) # Concatenate scalar data input_scalar = Concatenate()([ pockets_reshaped, clock_reshaped, turns_reshaped, castling_reshaped ]) # Reshape en_passants, promotion map to match piece_positions dimensions in_en_passants_2d_reshaped = Reshape( (nr_of_boards, 1, 8, 8))(in_en_passants_2d) in_promotion_map_2d_reshaped = Reshape( (nr_of_boards, 1, 8, 8))(in_promotion_map_2d) # Append en_passants, promotion map to piece positions boards_2d = Concatenate(axis=2, name="boards_2d")([ in_en_passants_2d_reshaped, in_promotion_map_2d_reshaped, in_piece_positions_2d ]) # Concatenate boards on last axis boards_2d_permuted = Permute((2, 3, 1, 4), name="boards_2d_permuted")(boards_2d) boards_2d_concatenated = Reshape( (2 * 6 + 2, 8, 8 * nr_of_boards), name="boards_2d_concatenated")(boards_2d_permuted) scalar_dense = Dense(256, activation=None, use_bias=False, name="scalar_dense")(input_scalar) scalar_dense_repeated = RepeatVector( 8 * 8 * nr_of_boards, name="scalar_dense_repeated")(scalar_dense) scalar_dense_permuted = Permute( (2, 1), name="scalar_dense_permuted")(scalar_dense_repeated) scalar_dense_2d = Reshape( (256, 8, 8 * nr_of_boards))(scalar_dense_permuted) conv_0 = Conv2D(256, (3, 3), data_format="channels_first", strides=1, padding="same", name="conv_0")(boards_2d_concatenated) added_scalars_0 = Add(name="added_scalars_0")( [conv_0, scalar_dense_2d]) if self.__use_batch_normalization: bn_0 = BatchNormalization(name="bn_0")(added_scalars_0) else: bn_0 = added_scalars_0 act_0 = LeakyReLU(name="act_0")(bn_0) current_layer = act_0 for i in np.arange(num_residual_blocks): current_layer = self.__residual_block(current_layer, block_id=i) head = current_layer # Policy head policy_conv = Conv2D(REL_MOVE_IDS, (3, 3), data_format="channels_first", strides=1, padding="same", name="policy_conv")(head) policy_flat = Reshape((REL_MOVE_IDS * 8 * 8 * nr_of_boards, ), name="policy_flat")(policy_conv) policy = Softmax(name="out_policy")(policy_flat) # Value head value_conv = Conv2D(1, (1, 1), data_format="channels_first", strides=1, padding="same", name="value_conv")(head) value_conv_act = LeakyReLU(name="value_conv_act")(value_conv) value_conv_flat = Flatten(name="value_conv_flat")(value_conv_act) value_dense = Dense(256, name="value_dense")(value_conv_flat) value_dense_act = LeakyReLU(name="value_dense_act")(value_dense) value = Dense(1, activation="tanh", name="out_value")(value_dense_act) return Model(inputs=[ in_en_passants_2d, in_piece_positions_2d, in_promotion_map_2d, in_pockets, in_turns, in_clocks, in_castling ], outputs=[policy, value])
def build_model(vectors, shape, settings): max_length, nr_hidden, nr_class = shape input1 = layers.Input(shape=(max_length,), dtype="int32", name="words1") input2 = layers.Input(shape=(max_length,), dtype="int32", name="words2") # embeddings (projected) embed = create_embedding(vectors, max_length, nr_hidden) a = embed(input1) b = embed(input2) # step 1: attend F = create_feedforward(nr_hidden) att_weights = layers.dot([F(a), F(b)], axes=-1) G = create_feedforward(nr_hidden) if settings["entail_dir"] == "both": norm_weights_a = layers.Lambda(normalizer(1))(att_weights) norm_weights_b = layers.Lambda(normalizer(2))(att_weights) alpha = layers.dot([norm_weights_a, a], axes=1) beta = layers.dot([norm_weights_b, b], axes=1) # step 2: compare comp1 = layers.concatenate([a, beta]) comp2 = layers.concatenate([b, alpha]) v1 = layers.TimeDistributed(G)(comp1) v2 = layers.TimeDistributed(G)(comp2) # step 3: aggregate v1_sum = layers.Lambda(sum_word)(v1) v2_sum = layers.Lambda(sum_word)(v2) concat = layers.concatenate([v1_sum, v2_sum]) elif settings["entail_dir"] == "left": norm_weights_a = layers.Lambda(normalizer(1))(att_weights) alpha = layers.dot([norm_weights_a, a], axes=1) comp2 = layers.concatenate([b, alpha]) v2 = layers.TimeDistributed(G)(comp2) v2_sum = layers.Lambda(sum_word)(v2) concat = v2_sum else: norm_weights_b = layers.Lambda(normalizer(2))(att_weights) beta = layers.dot([norm_weights_b, b], axes=1) comp1 = layers.concatenate([a, beta]) v1 = layers.TimeDistributed(G)(comp1) v1_sum = layers.Lambda(sum_word)(v1) concat = v1_sum H = create_feedforward(nr_hidden) out = H(concat) out = layers.Dense(nr_class, activation="softmax")(out) model = Model([input1, input2], out) model.compile( optimizer=optimizers.Adam(lr=settings["lr"]), loss="categorical_crossentropy", metrics=["accuracy"], ) return model
def get_base_model(input_shape=(768, 432, 6), compiled=True): ### ResNet Backbone ### inp = Input(shape=input_shape) img, bgr = Lambda(lambda x: tf.split(x, 2, axis=-1))(inp) #reduced_channels = Conv3D(filters=512, kernel_size=(3,3,2),padding='SAME')(inp) resnet = ResNet101(weights='imagenet', include_top=False, input_tensor=img) resnet.trainable = False backbone_in = resnet.input resblock1 = resnet.get_layer('conv1_relu').output resblock2 = resnet.get_layer('conv2_block3_out').output resblock3 = resnet.get_layer('conv3_block4_out').output backbone_out = resnet.output x = backbone_out #x = Add()([img_res,bgr_res]) ### ASPP ### # conv block 1 conv1 = Conv2D(ASPP_FILTERS, 1, padding='SAME', dilation_rate=1, use_bias=False, name='aspp_conv1_in')(x) conv1 = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(conv1) conv1 = ReLU()(conv1) # conv block 2 conv2 = Conv2D(ASPP_FILTERS, 3, padding='SAME', dilation_rate=ASPP_DILATIONS[0], use_bias=False, name='aspp_conv2_in')(x) conv2 = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(conv2) conv2 = ReLU()(conv2) # conv block 3 conv3 = Conv2D(ASPP_FILTERS, 3, padding='SAME', dilation_rate=ASPP_DILATIONS[1], use_bias=False, name='aspp_conv3_in')(x) conv3 = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(conv3) conv3 = ReLU()(conv3) # conv block 4 conv4 = Conv2D(ASPP_FILTERS, 3, padding='SAME', dilation_rate=ASPP_DILATIONS[2], use_bias=False, name='aspp_conv4_in')(x) conv4 = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(conv4) conv4 = ReLU()(conv4) # pooling block dims = tf.shape(backbone_out)[1], tf.shape(x)[2] pool = GlobalAveragePooling2D(name='aspp_pool_in')(x) pool = pool[:, None, None, :] pool = Conv2D(ASPP_FILTERS, 1, use_bias=False)(pool) pool = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(pool) pool = ReLU()(pool) pool = tf.image.resize(pool, dims, 'nearest') # pyramid construction pyr = tf.concat([conv1, conv2, conv3, conv4, pool], axis=-1) pyr = Conv2D(ASPP_FILTERS, 1, use_bias=False)(pyr) pyr = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(pyr) pyr = ReLU()(pyr) pyr = Dropout(DROPOUT_RATE)(pyr) ### DECODER ### x4, x3, x2, x1, x0 = pyr, resblock3, resblock2, resblock1, backbone_in x = Lambda(lambda a: tf.image.resize(a, tf.shape(x3)[1:3]))(x4) x = tf.concat([x, x3], axis=-1) x = Conv2D(DECODER_CHANNELS[0], 3, padding='SAME', use_bias=False)(x) x = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(x) x = ReLU()(x) x = Lambda(lambda a: tf.image.resize(a, tf.shape(x2)[1:3]))(x) x = tf.concat([x, x2], axis=-1) x = Conv2D(DECODER_CHANNELS[1], 3, padding='SAME', use_bias=False)(x) x = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(x) x = ReLU()(x) x = Lambda(lambda a: tf.image.resize(a, tf.shape(x1)[1:3]))(x) x = tf.concat([x, x1], axis=-1) x = Conv2D(DECODER_CHANNELS[2], 3, padding='SAME', use_bias=False)(x) x = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON)(x) x = ReLU()(x) x = Lambda(lambda a: tf.image.resize(a, tf.shape(x0)[1:3]))(x) x = tf.concat([x, x0], axis=-1) x = Conv2D(DECODER_CHANNELS[3], 3, padding='SAME', use_bias=True)(x) print(x.shape) out = x ### COMPILE ### model = Model(inputs=inp, outputs=out) if compiled: model.compile(optimizer='adam', loss=base_alpha_mse_loss) return model
def build(self, sentence_length, word_length, num_labels, num_intent_labels, word_vocab_size, char_vocab_size, word_emb_dims=100, char_emb_dims=25, char_lstm_dims=25, tagger_lstm_dims=100, dropout=0.2, embedding_matrix=None): """ Build a model Args: sentence_length (int): max sentence length word_length (int): max word length (in characters) num_labels (int): number of slot labels num_intent_labels (int): number of intent classes word_vocab_size (int): word vocabulary size char_vocab_size (int): character vocabulary size word_emb_dims (int, optional): word embedding dimensions char_emb_dims (int, optional): character embedding dimensions char_lstm_dims (int, optional): character feature LSTM hidden size tagger_lstm_dims (int, optional): tagger LSTM hidden size dropout (float, optional): dropout rate embedding_matrix (dict, optional): external word embedding dictionary """ if embedding_matrix is not None: # load pre-trained word embeddings into an Embedding layer # note that we set trainable = False so as to keep the embeddings fixed embedding_layer = Embedding(word_vocab_size, word_emb_dims, weights=[embedding_matrix], input_length=sentence_length, trainable=True, name='word_embedding_layer') else: # learn embeddings ourselves embedding_layer = Embedding(word_vocab_size, word_emb_dims, input_length=sentence_length, name='word_embedding_layer') # create word embedding input and embedding layer words_input = Input(shape=(sentence_length,), name='words_input') word_embeddings = embedding_layer(words_input) word_embeddings = Dropout(dropout)(word_embeddings) # create word character input and embeddings layer word_chars_input = Input(shape=(sentence_length, word_length), name='word_chars_input') char_embedding_layer = Embedding(char_vocab_size, char_emb_dims, input_length=word_length, name='char_embedding_layer') # apply embedding to each word char_embeddings = TimeDistributed(char_embedding_layer)(word_chars_input) # feed dense char vectors into BiLSTM char_embeddings = TimeDistributed(Bidirectional(LSTM(char_lstm_dims)))(char_embeddings) char_embeddings = Dropout(dropout)(char_embeddings) # first BiLSTM layer (used for intent classification) first_bilstm_layer = Bidirectional( LSTM(tagger_lstm_dims, return_sequences=True, return_state=True)) first_lstm_out = first_bilstm_layer(word_embeddings) lstm_y_sequence = first_lstm_out[:1][0] # save y states of the LSTM layer states = first_lstm_out[1:] hf, cf, hb, cb = states # extract last hidden states h_state = concatenate([hf, hb], axis=-1) intent_out = Dense(num_intent_labels, activation='softmax', name='intent_classifier_output')(h_state) # create the 2nd feature vectors combined_features = concatenate([lstm_y_sequence, char_embeddings], axis=-1) # 2nd BiLSTM layer for label classification second_bilstm_layer = Bidirectional( LSTM(tagger_lstm_dims, return_sequences=True))(combined_features) second_bilstm_layer = Dropout(dropout)(second_bilstm_layer) # feed BiLSTM vectors into CRF crf = CRF(num_labels, sparse_target=False) labels_out = crf(second_bilstm_layer) # compile the model model = Model(inputs=[words_input, word_chars_input], outputs=[intent_out, labels_out]) # define losses and metrics loss_f = {'intent_classifier_output': 'categorical_crossentropy', 'crf_1': crf.loss_function} metrics = {'intent_classifier_output': 'categorical_accuracy', 'crf_1': crf.accuracy} model.compile(loss=loss_f, optimizer='adam', metrics=metrics) self.model = model
for image in data["Path"]: test_data.append( cv2.resize(cv2.imread(image), dsize=(224, 224), interpolation=cv2.INTER_CUBIC) / 255) #Resize and normalize test_data = np.array(test_data) label_encoder_test = LabelEncoder() integer_encoded_test = label_encoder_test.fit_transform(data["Label"]) onehot_encoder_test = OneHotEncoder(sparse=False) integer_encoded_test = integer_encoded_test.reshape(len(integer_encoded_test), 1) Y_test = onehot_encoder_test.fit_transform(integer_encoded_test) #Collect the logits from the previous layer output and store it in a different model teacher_WO_Softmax = Model(loaded_model.input, loaded_model.get_layer('dense_4').output) # Define a manual softmax function def softmax(x): return np.exp(x) / (np.exp(x).sum()) teacher_test_logits = teacher_WO_Softmax.predict( test_data ) # This model directly gives the logits ( see the teacher_WO_softmax model above) Y_test_soft = softmax(teacher_test_logits / temp) Y_test_new = np.concatenate([Y_test, Y_test_soft], axis=1)
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Problems with tensorflow2 # from keras import Input, Model # from keras.layers import Dense from keras import Input, Model from keras.layers import Dense # Create the input vector (13 elements). inputs = Input((13, )) # Create the first (input) layer (10 nodes) and connect it to the input vector. input = Dense(10)(inputs) # Create the next (hidden) layer (10 nodes) and connect it to the input layer. hidden = Dense(10)(input) # Create the output layer (1 node) and connect it to the previous (hidden) layer. output = Dense(1)(hidden) # Now let's create the neural network, specifying the input layer and output layer. model = Model(inputs, output)
def score_and_write_batch(model: keras.Model, file_out: TextIO, batch_size: int, python_batch_size: int, tensor_type: str, annotation_set: str, window_size: int, read_limit: int, tensor_dir: str = '') -> None: """Score a batch of variants with a CNN model. Write tab delimited temp file with scores. This function is tightly coupled with the CNNScoreVariants.java It requires data written to the fifo in the order given by transferToPythonViaFifo Arguments model: a keras model file_out: The temporary VCF-like file where variants scores will be written batch_size: The total number of variants available in the fifo python_batch_size: the number of variants to process in each inference tensor_type: The name for the type of tensor to make annotation_set: The name for the set of annotations to use window_size: The size of the context window of genomic bases, i.e the width of the tensor read_limit: The maximum number of reads to encode in a tensor, i.e. the height of the tensor tensor_dir : If this path exists write hd5 files for each tensor (optional for debugging) """ annotation_batch = [] reference_batch = [] variant_types = [] variant_data = [] read_batch = [] for _ in range(batch_size): fifo_line = tool.readDataFIFO() fifo_data = fifo_line.split(defines.DATA_TYPE_SEPARATOR) variant_data.append(fifo_data[CONTIG_FIFO_INDEX] + defines.DATA_TYPE_SEPARATOR + fifo_data[POS_FIFO_INDEX] + defines.DATA_TYPE_SEPARATOR + fifo_data[REF_FIFO_INDEX] + defines.DATA_TYPE_SEPARATOR + fifo_data[ALT_FIFO_INDEX]) reference_batch.append(reference_string_to_tensor(fifo_data[REF_STRING_FIFO_INDEX])) annotation_batch.append(annotation_string_to_tensor(annotation_set, fifo_data[ANNOTATION_FIFO_INDEX])) variant_types.append(fifo_data[VARIANT_TYPE_FIFO_INDEX].strip()) fifo_idx = VARIANT_FIFO_FIELDS if tensor_type in defines.TENSOR_MAPS_2D and len(fifo_data) > fifo_idx: read_tuples = [] var = Variant(fifo_data[CONTIG_FIFO_INDEX], int(fifo_data[POS_FIFO_INDEX]), fifo_data[POS_FIFO_INDEX], fifo_data[ALT_FIFO_INDEX], fifo_data[VARIANT_TYPE_FIFO_INDEX]) while fifo_idx+READ_ELEMENTS <= len(fifo_data): read_tuples.append( Read(fifo_data[fifo_idx + READ_BASES_FIFO_INDEX], list(map(int, fifo_data[fifo_idx+READ_QUAL_FIFO_INDEX].split(defines.DATA_VALUE_SEPARATOR))), fifo_data[fifo_idx+READ_CIGAR_FIFO_INDEX], bool_from_java(fifo_data[fifo_idx+READ_REVERSE_FIFO_INDEX]), bool_from_java(fifo_data[fifo_idx+READ_MATE_REVERSE_FIFO_INDEX]), bool_from_java(fifo_data[fifo_idx+READ_FIRST_IN_PAIR_FIFO_INDEX]), int(fifo_data[fifo_idx+READ_MQ_FIFO_INDEX]), int(fifo_data[fifo_idx+READ_REF_START_FIFO_INDEX]))) fifo_idx += READ_ELEMENTS _, ref_start, _ = get_variant_window(window_size, var) insert_dict = get_inserts(read_tuples, var, window_size) tensor = read_tuples_to_tensor(read_tuples, ref_start, insert_dict, tensor_type, window_size, read_limit) reference_sequence_into_tensor(fifo_data[4], tensor, insert_dict, window_size, read_limit) if os.path.exists(tensor_dir): _write_tensor_to_hd5(tensor, annotation_batch[-1], fifo_data[0], fifo_data[1], fifo_data[6], tensor_type, annotation_set, tensor_dir) read_batch.append(tensor) if tensor_type in defines.TENSOR_MAPS_1D: predictions = model.predict([np.array(reference_batch), np.array(annotation_batch)], batch_size=python_batch_size) elif tensor_type in defines.TENSOR_MAPS_2D: predictions = model.predict( [np.array(read_batch), np.array(annotation_batch)], batch_size=python_batch_size) else: raise ValueError('Unknown tensor mapping. Check architecture file.', tensor_type) indel_scores = predictions_to_indel_scores(predictions) snp_scores = predictions_to_snp_scores(predictions) for i in range(batch_size): if 'SNP' == variant_types[i]: file_out.write(variant_data[i] + defines.DATA_TYPE_SEPARATOR + '{0:.3f}'.format(snp_scores[i]) + '\n') elif 'INDEL' == variant_types[i]: file_out.write(variant_data[i] + defines.DATA_TYPE_SEPARATOR + '{0:.3f}'.format(indel_scores[i]) + '\n') else: file_out.write(variant_data[i] + defines.DATA_TYPE_SEPARATOR + '{0:.3f}'.format(max(snp_scores[i], indel_scores[i])) + '\n')