def train(s_data, s_out, nuc, bestwight, samplesize, epoch_num): batch_size = 1024 num_classes_org = 1024 num_classes = 63 shape1 = (None, DATA_LENGTH, 2) model = cnn_network_keras.build_network(shape=shape1, num_classes=num_classes_org) model.load_weights(bestwight) model.layers.pop() # remove last layer model.layers.pop() # remove last layer model.layers.pop() # remove last layer for layer in model.layers: if layer.name == "conv1d_20": break else: layer.trainable = False flat = GlobalAveragePooling1D()(model.layers[-1].output) model_t = Model(inputs=model.input, outputs=flat) model_r = Network(inputs=model_t.input, outputs=model_t.output, name="shared_layer") prediction = Dense(num_classes, activation='softmax')(model_t.output) model_r = Model(inputs=model_r.input, outputs=prediction) optimizer = SGD(lr=5e-5, decay=0.00005) model_r.compile(optimizer=optimizer, loss="categorical_crossentropy") model_t.compile(optimizer=optimizer, loss=original_loss) x_ref, test_x_r, y_ref, test_y_r, num_classes_r = prepDataNear( s_data, samplesize, nuc) x_target, test_x, train_y, test_y, num_classes = prepData( s_data, samplesize, nuc) ref_samples = np.arange(x_ref.shape[0]) loss, loss_c = [], [] epochs = [] print("training...") for epochnumber in range(epoch_num): x_r, y_r, lc, ld = [], [], [], [] np.random.shuffle(x_target) np.random.shuffle(ref_samples) for i in range(len(x_target)): x_r.append(x_ref[ref_samples[i]]) y_r.append(y_ref[ref_samples[i]]) x_r = np.array(x_r) y_r = np.array(y_r) for i in range(int(len(x_target) / batchsize)): batch_target = x_target[i * batchsize:i * batchsize + batchsize] batch_ref = x_r[i * batchsize:i * batchsize + batchsize] batch_y = y_r[i * batchsize:i * batchsize + batchsize] # target data lc.append( model_t.train_on_batch(batch_target, np.zeros((batchsize, feature_out)))) # reference data ld.append(model_r.train_on_batch(batch_ref, batch_y)) loss.append(np.mean(ld)) loss_c.append(np.mean(lc)) epochs.append(epochnumber) print("epoch : {} ,Descriptive loss : {}, Compact loss : {}".format( epochnumber + 1, loss[-1], loss_c[-1])) if epochnumber % 1 == 0: model_t.save_weights(s_out + '/' + nuc + '/model_t_ep_{}.h5'.format(epochnumber)) model_r.save_weights(s_out + '/' + nuc + '/model_r_ep_{}.h5'.format(epochnumber))
def train(x_target, x_ref, y_ref, epoch_num): print("Model build...") # mobile = VGG16(include_top=False, input_shape=input_shape, weights='imagenet', pooling= 'avg') mobile = InceptionResNetV2(include_top=False, input_shape=input_shape, weights='imagenet') # mobile = MobileNetV2(include_top=True, input_shape=input_shape, alpha=alpha, # weights='imagenet') mobile.layers.pop() ''' Last layer : - block_13_expand : Mobilenet v2 - block5_conv1 : VGG16 - mixed_7a : Inception resnetv2 ''' # for layer in mobile.layers: # print(layer.name) # if layer.name == "block_13_expand": # "block5_conv1": for VGG16 # if layer.name == "block5_conv1": # if layer.name == "mixed_7a": # break # else: # layer.trainable = False # exit() flat = GlobalAveragePooling2D()(mobile.layers[-1].output) model_t = Model(inputs=mobile.input, outputs=flat) model_r = Network(inputs=model_t.input, outputs=model_t.output, name="shared_layer") prediction = Dense(classes, activation='softmax')(model_t.output) model_r = Model(inputs=model_r.input, outputs=prediction) optimizer = SGD(lr=5e-5, decay=0.00005) model_r.compile(optimizer=optimizer, loss="categorical_crossentropy") model_t.compile(optimizer=optimizer, loss=original_loss) model_t.summary() ref_samples = np.arange(x_ref.shape[0]) loss, loss_c = [], [] epochs = [] print("training...") for epochnumber in range(epoch_num): x_r, y_r, lc, ld = [], [], [], [] np.random.shuffle(x_target) np.random.shuffle(ref_samples) for i in range(len(x_target)): x_r.append(x_ref[ref_samples[i]]) y_r.append(y_ref[ref_samples[i]]) x_r = np.array(x_r) y_r = np.array(y_r) for i in range(int(len(x_target) / batchsize)): batch_target = x_target[i * batchsize:i * batchsize + batchsize] batch_ref = x_r[i * batchsize:i * batchsize + batchsize] batch_y = y_r[i * batchsize:i * batchsize + batchsize] #target data lc.append( model_t.train_on_batch(batch_target, np.zeros((batchsize, feature_out)))) #reference data ld.append(model_r.train_on_batch(batch_ref, batch_y)) loss.append(np.mean(ld)) loss_c.append(np.mean(lc)) epochs.append(epochnumber) print("epoch : {} ,Descriptive loss : {}, Compact loss : {}".format( epochnumber + 1, loss[-1], loss_c[-1])) if epochnumber % 10 == 0: model_t.save_weights('model/model_t_smd_{}.h5'.format(epochnumber)) model_r.save_weights('model/model_r_smd_{}.h5'.format(epochnumber))
def train_test(result_df, df_t, df_r, df_test): # data x_target = np.array(df_t.vecs.to_list()) x_ref = np.array(df_r.vecs.to_list()) y_ref = np.array(df_r.target.to_list()) y_ref = to_categorical(y_ref) test_vecs = np.array(df_test.vecs.to_list()) n_sup = 10000 n_per_targ = 1000 df_r_temp = df_r.groupby('target', group_keys=False).apply( lambda df: df.sample(n=min(df.shape[0], n_per_targ), random_state=42)) x_tr = np.array(df_t.head(n_sup).append(df_r_temp).vecs.to_list()) y_tr = np.array(df_t.head(n_sup).append(df_r_temp).label.to_list()) #y_tr = to_categorical(y_tr) #print(f"{df.where(df.label == 0).dropna().target.value_counts()}") #print(f"x_target: {x_target.shape}\nx_ref: {x_ref.shape}\ny_ref: {y_ref.shape}\n") res_path = "/home/philipp/projects/dad4td/reports/one_class/all.tsv" classes = df_r.target.unique().shape[0] print(f"classes: {classes}") batchsize = 128 epoch_num = 15 epoch_report = 5 feature_out = 64 pred_mode = "nn" # get the loss for compactness original_loss = create_loss(classes, batchsize) # model creation model = create_model(loss="binary_crossentropy", n_in=x_target[0].shape[0]) model_t = Model(inputs=model.input, outputs=model.output) model_r = Network(inputs=model_t.input, outputs=model_t.output, name="shared_layer") prediction = Dense(classes, activation='softmax')(model_t.output) model_r = Model(inputs=model_r.input, outputs=prediction) #latent_t = Dense(2, activation='relu')(model_t.output) #model_t = Model(inputs=model_t.input,outputs=latent_t) prediction_t = Dense(feature_out, activation='softmax')(model_t.output) model_t = Model(inputs=model_t.input, outputs=prediction_t) #optimizer = SGD(lr=5e-5, decay=0.00005) optimizer = Adam(learning_rate=5e-5) model_r.compile(optimizer=optimizer, loss="categorical_crossentropy") model_t.compile(optimizer=optimizer, loss=original_loss) model_t.summary() model_r.summary() ref_samples = np.arange(x_ref.shape[0]) loss, loss_c = [], [] epochs = [] best_acc = 0 print("training...") for epochnumber in range(epoch_num): x_r, y_r, lc, ld = [], [], [], [] np.random.shuffle(x_target) np.random.shuffle(ref_samples) for i in range(len(x_ref)): x_r.append(x_ref[ref_samples[i]]) y_r.append(y_ref[ref_samples[i]]) x_r = np.array(x_r) y_r = np.array(y_r) for i in range(int(len(x_target) / batchsize)): batch_target = x_target[i * batchsize:i * batchsize + batchsize] batch_ref = x_r[i * batchsize:i * batchsize + batchsize] batch_y = y_r[i * batchsize:i * batchsize + batchsize] # target data lc.append( model_t.train_on_batch(batch_target, np.zeros((batchsize, feature_out)))) # reference data ld.append(model_r.train_on_batch(batch_ref, batch_y)) loss.append(np.mean(ld)) loss_c.append(np.mean(lc)) epochs.append(epochnumber) if epochnumber % epoch_report == 0 or epochnumber == epoch_num - 1: print( f"-----\n\nepoch : {epochnumber+1} ,Descriptive loss : {loss[-1]}, Compact loss : {loss_c[-1]}" ) model_t.save_weights( '/home/philipp/projects/dad4td/models/one_class/model_t_smd_{}.h5' .format(epochnumber)) model_r.save_weights( '/home/philipp/projects/dad4td/models/one_class/model_r_smd_{}.h5' .format(epochnumber)) #test_b = model_t.predict(test_vecs) #od = OCSVM() # od.fit(test_b) #decision_scores = od.labels_ # decision_scores = decision_scores.astype(float) labels = df_test["label"].astype(int).values # threshold = 0.5 # scores = get_scores(dict(),labels, np.where(decision_scores > threshold, 0, 1), outlabel=0) if pred_mode == "svm": x_tr_pred = model_t.predict(x_tr) clf = SVC() clf.fit(x_tr_pred, y_tr) preds = model_t.predict(test_vecs) preds = clf.predict(preds) elif pred_mode == "nn": y_tr = y_tr.astype(int) print(y_tr) x_tr_pred = model_t.predict(x_tr) clf = create_sup_model(n_in=feature_out) clf.summary() clf.fit(x_tr_pred, y=y_tr, epochs=15, batch_size=64, verbose=True) decision_scores = model_t.predict(test_vecs) decision_scores = clf.predict(decision_scores) preds = decision_scores.astype(float) _ = plt.hist(preds, bins=10) plt.show() else: raise Exception(f"{pred_mode} must be one of svm, nn, osvm") scores = get_scores(dict(), labels, preds, outlabel=0) print(f"\n\nTest scores:\n{pd.DataFrame([scores], index=[0])}") if scores["accuracy"] > best_acc: best_acc = scores["accuracy"] print(f"best_acc updated to: {best_acc}") normalize = "true" print(f"{confusion_matrix(labels, preds, normalize=normalize)}") result_df = result_df.append(dict(cclass=list(df_test.target.unique()), accuracy=best_acc), ignore_index=True) result_df.to_csv(res_path, sep="\t") return result_df