def train_model(self, model, should_save_weights=False): optimizer = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy']) X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = utils.load_datasets() # Normalize image vectors X_train = X_train_orig / 255. X_test = X_test_orig / 255. Y_train = utils.convert_to_one_hot(Y_train_orig, 5).T Y_test = utils.convert_to_one_hot(Y_test_orig, 5).T print ("number of training examples = " + str(X_train.shape[0])) print ("number of test examples = " + str(X_test.shape[0])) print ("X_train shape: " + str(X_train.shape)) print ("Y_train shape: " + str(Y_train.shape)) print ("X_test shape: " + str(X_test.shape)) print ("Y_test shape: " + str(Y_test.shape)) model.fit(X_train, Y_train, epochs=5, batch_size=32) preds = model.evaluate(X_test, Y_test) print ("Loss = " + str(preds[0])) print ("Test Accuracy = " + str(preds[1])) model.save("resnet-50.h5") if should_save_weights: model.save_weights("resnet-50.weights.h5")
def data_prepare(num_class): #data_prepare img = scio.loadmat('./Datasets/IndianPines/Indian_pines_corrected.mat' )['indian_pines_corrected'] gt = scio.loadmat( './Datasets/IndianPines/Indian_pines_gt.mat')['indian_pines_gt'] h, w = gt.shape[0], gt.shape[1] c = img.shape[2] idx = np.ones((img.shape[0], img.shape[1])) idx = np.where(idx == 1) idx_x = np.resize(idx[0], (img.shape[0], img.shape[1], 1)) idx_y = np.resize(idx[1], (img.shape[0], img.shape[1], 1)) img_with_idx = np.concatenate((img, idx_x), axis=2) img_with_idx = np.concatenate((img_with_idx, idx_y), axis=2) gt_train = np.zeros((img.shape[0], img.shape[1])) for i in range(1, 17): id = np.where(gt == i) num = id[0].shape[0] if num >= 50: a = np.ones(num) a = np.where(a == 1) a = list(a[0]) idx_rand = random.sample(a, 50) else: a = np.ones(num) a = np.where(a == 1) a = list(a[0]) idx_rand = random.sample(a, 15) for item in idx_rand: x = id[0][item] y = id[1][item] gt_train[x][y] = i gt_test = gt - gt_train gt_train = np.resize(gt_train, (gt_train.shape[0] * gt_train.shape[1])) gt_test = np.resize(gt_test, (gt_test.shape[0] * gt_test.shape[1])) img_with_idx = np.resize(img_with_idx, (h * w, c + 2)) img_train = img_with_idx[gt_train > 0, :] img_test = img_with_idx[gt_test > 0, :] tr_gt = gt_train[gt_train > 0].astype(int) te_gt = gt_test[gt_test > 0].astype(int) s2D_tr_spe, s2D_tr_spa = Mat_dis_s2(img_train) s2D_te_spe, s2D_te_spa = Mat_dis_s2(img_test) H_tr_spe = construct_H_with_KNN_from_distance(s2D_tr_spe, 20) H_tr_spa = construct_H_with_KNN_from_distance(s2D_tr_spa, 10) H_te_spe = construct_H_with_KNN_from_distance(s2D_te_spe, 20) H_te_spa = construct_H_with_KNN_from_distance(s2D_te_spa, 10) H_tr = np.concatenate((H_tr_spe, H_tr_spa), axis=1) H_te = np.concatenate((H_te_spe, H_te_spa), axis=1) # GHy_tr = _generate_G_from_H(H_tr) # GHy_te = _generate_G_from_H(H_te) G_tr = generate_Graph(H_tr_spe) G_te = generate_Graph(H_te_spe) tr_gt = convert_to_one_hot(tr_gt - 1, num_class) te_gt = convert_to_one_hot(te_gt - 1, num_class) tr_gt = tr_gt.T te_gt = te_gt.T return img_train, img_test, tr_gt, te_gt, G_tr, G_te
def __getitem__(self, index): img_x, img_y = self.imgs[index] shift = 10 rotate = 15 scale = 0.2 intensity = 0.1 flip = True img_x = np.expand_dims(img_x, axis=0) img_x = img_x.transpose([0, 2, 3, 1]) img_y = np.expand_dims(img_y, axis=0) if self.augment and np.random.uniform() > 0.5: img_x, img_y = data_augmenter(img_x, img_y, shift=shift, rotate=rotate, scale=scale, intensity=intensity, flip=flip) labels_onehot = convert_to_one_hot(img_y).astype(np.float32) labels_onehot = labels_onehot.transpose([1, 0, 2, 3]) img_x = img_x.transpose([0, 3, 1, 2]) img_x = torch.from_numpy(img_x).float() img_y = torch.from_numpy(labels_onehot) return img_x.squeeze(), img_y.squeeze()
def __getitem__(self, index): img_x, img_y = self.imgs[index] shift = 10 rotate = 15 scale = 0.2 intensity = 0.1 flip = True if self.augment: aug = A.Compose([ A.ElasticTransform(alpha=200, sigma=200 * 0.05, alpha_affine=200 * 0.03, p=0.4), # A.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=50, val_shift_limit=50), # A.RandomBrightnessContrast(), # A.RandomGamma(), # A.CLAHE(), # A.RGBShift(p=0.2), # A.Blur(blur_limit=3), # A.GaussNoise(p=0.2), # A.Flip(), # A.RandomRotate90(), ]) img_x = img_x.transpose([1, 2, 0]) augmented = aug(image=img_x, mask=img_y) img_x = augmented['image'] img_y = augmented['mask'] img_x = img_x.transpose([2, 0, 1]) img_x = np.expand_dims(img_x, axis=0) img_x = img_x.transpose([0, 2, 3, 1]) img_y = np.expand_dims(img_y, axis=0) if self.augment: img_x, img_y = data_augmenter(img_x, img_y, shift=shift, rotate=rotate, scale=scale, intensity=intensity, flip=flip) # img_x = np.expand_dims(img_x, axis=0) # img_x = img_x.transpose([0, 2, 3, 1]) # img_y = np.expand_dims(img_y, axis=0) labels_onehot = convert_to_one_hot(img_y).astype(np.float32) labels_onehot = labels_onehot.transpose([1, 0, 2, 3]) M = img_x.copy() M[img_y == 0] = 0 # cv2.imwrite('./test.png', M.squeeze()*255) M = M.transpose([0, 3, 1, 2]) M = torch.from_numpy(M).float() img_x = img_x.transpose([0, 3, 1, 2]) img_x = torch.from_numpy(img_x).float() img_y = torch.from_numpy(labels_onehot) return img_x.squeeze(), img_y.squeeze(), M.squeeze()
def testModel(self): model = load_model("resnet-50_100.h5") X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = utils.load_datasets() X_train = X_train_orig / 255. X_test = X_test_orig / 255. Y_train = utils.convert_to_one_hot(Y_train_orig, 5).T Y_test = utils.convert_to_one_hot(Y_test_orig, 5).T predsTest = model.evaluate(X_test, Y_test) predsTrain = model.evaluate(X_train, Y_train) print ("Test Loss = " + str(predsTest[0])) print ("Test Accuracy = " + str(predsTest[1])) print ("Train Loss = " + str(predsTrain[0])) print ("Train Accuracy = " + str(predsTrain[1]))
def test(): # model.eval() output = pass_data_iteratively(idx_test, test_idx) predicted = torch.max(output, dim=1)[1] y_pred = predicted.data.cpu().numpy().tolist() test_labels = labels[idx_test].data.cpu().numpy().tolist() print('=====================================') print( classification_report(test_labels, y_pred, target_names=target_names, digits=5)) t_labels = convert_to_one_hot(labels[idx_test].unsqueeze(1).cpu(), 4).cuda() if args.dataset == 'weibo': t_labels = convert_to_one_hot(labels[idx_test].unsqueeze(1).cpu(), 2).cuda() result_test = evaluation_4class(output, t_labels) return result_test
def train(self): print("[*] Checking if previous run exists in {}" "".format(self.model_dir)) latest_checkpoint = tf.train.latest_checkpoint(self.model_dir) if tf.train.latest_checkpoint(self.model_dir) is not None: print("[*] Saved result exists! loading...") self.saver.restore(self.sess, latest_checkpoint) print("[*] Loaded previously trained weights") self.b_pretrain_loaded = True else: print("[*] No previous result") self.b_pretrain_loaded = False print("[*] Training starts...") self.model_summary_writer = None ##Training for n_epoch in trange(self.num_epoch, desc="Training[epoch]"): self.data_loader.reset_batch_pointer(0) for k in trange(self.data_loader.sizes[0], desc="[per_batch]"): # Fetch training data batch_x, batch_y = self.data_loader.next_batch(0) batch_x_onehot = convert_to_one_hot(batch_x, self.config.num_node) if self.config.model_type == 'lstm': reshaped = batch_x_onehot.reshape([ self.config.batch_size, self.config.num_node, self.config.num_time_steps ]) batch_x = reshaped elif self.config.model_type == 'glstm': reshaped = batch_x_onehot.reshape([ self.config.batch_size, self.config.num_time_steps, 1, self.config.num_node ]) batch_x = np.transpose(reshaped, (0, 3, 2, 1)) feed_dict = { self.model.rnn_input: batch_x, self.model.rnn_output: batch_y } res = self.model.train(self.sess, feed_dict, self.model_summary_writer, with_output=True) self.model_summary_writer = self._get_summary_writer(res) if n_epoch % 10 == 0: self.saver.save(self.sess, self.model_dir) print(batch_x, batch_y)
def predict_with_cnn2(): _, _, x_test, y_test = utils.load_data() x_test = x_test.astype('float32') mean = np.load('x_train_mean.npy') std = np.load('x_train_std.npy') x_test = (x_test - mean) / (std + 1e-7) y_test = utils.convert_to_one_hot(y_test, 10) model = load_model('model_cnn_v2.h5') preds = model.evaluate(x_test, y_test) print("Loss = " + str(preds[0])) print("Accuracy = " + str(preds[1])) model.summary() # plot_model(model, to_file='model_cnn_v2.png', show_shapes=True) y_prob = model.predict(x_test) y_pred = [x for x in np.argmax(y_prob, axis=1)] y_truth = [x for x in np.argmax(y_test, axis=1)] print(confusion_matrix(y_truth, y_pred)) print( classification_report(y_truth, y_pred, target_names=utils.label_names))
def test(self): self.model_summary_writer = None #Testing for n_sample in trange(self.data_loader.sizes[2], desc="Testing"): batch_x, batch_y = self.data_loader.next_batch(2) batch_x_onehot = convert_to_one_hot(batch_x, self.config.num_node) reshaped = batch_x_onehot.reshape([ self.config.batch_size, self.config.num_time_steps, 1, self.config.num_node ]) batch_x = np.transpose(reshaped, (0, 3, 2, 1)) feed_dict = { self.model.rnn_input: batch_x, self.model.rnn_output: batch_y } res = self.model.test(self.sess, feed_dict, self.model_summary_writer, with_output=True) self.model_summary_writer = self._get_summary_writer(res)
def predict_with_dnn(): _, _, x_test, y_test = utils.load_data() x_test = x_test.astype('float32') x_test = x_test.reshape(x_test.shape[0], -1) scaler_model = pickle.load(open('scaler_model.sav', 'rb')) pca_model = pickle.load(open('pca_model.sav', 'rb')) x_test = scaler_model.transform(x_test) x_test = pca_model.transform(x_test) y_test = utils.convert_to_one_hot(y_test, 10) model = load_model('model_dnn.h5') preds = model.evaluate(x_test, y_test) print("Loss = " + str(preds[0])) print("Accuracy = " + str(preds[1])) model.summary() # plot_model(model, to_file='model_dnn.png', show_shapes=True) y_prob = model.predict(x_test) y_pred = [x for x in np.argmax(y_prob, axis=1)] y_truth = [x for x in np.argmax(y_test, axis=1)] print(confusion_matrix(y_truth, y_pred)) print( classification_report(y_truth, y_pred, target_names=utils.label_names))
def get_epoch_batch(data_list, batch_size, iteration, idx, image_size=192, data_augmentation=False, shift=0.0, rotate=0.0, scale=0.0, intensity=0.0, flip=False, norm=True): images, labels = [], [] for i in range(iteration * batch_size, (iteration + 1) * batch_size): image_name, label_name = data_list[idx[i]] if os.path.exists(image_name) and os.path.exists(label_name): # print(' Select {0} {1}'.format(image_name, label_name)) # Read image and label image = np.load(image_name) label = np.load(label_name) # Handle exceptions if image.shape != label.shape: print( 'Error: mismatched size, image.shape = {0}, label.shape = {1}' .format(image.shape, label.shape)) print('Skip {0}, {1}'.format(image_name, label_name)) continue if image.max() < 1e-6: print('Error: blank image, image.max = {0}'.format( image.max())) print('Skip {0} {1}'.format(image_name, label_name)) continue # Append the image slices to the batch # Use list for appending, which is much faster than numpy array Z = image.shape[0] if Z > 8: r = Z - 8 start = random.randint(0, r) for z in range(start, start + 8): temp = image[z, :, :] if norm: temp = rescale_intensity(temp, (0.5, 99.5)) images += [temp] labels += [label[z, :, :]] else: for z in range(Z): temp = image[z, :, :] if norm: temp = rescale_intensity(temp, (0.5, 99.5)) images += [temp] labels += [label[z, :, :]] # Convert to a numpy array images = np.array(images, dtype=np.float32) labels = np.array(labels, dtype=np.float32) # Add the channel dimension # tensorflow by default assumes NHWC format (batch_size, 128, 128, 1) images = np.expand_dims(images, axis=3) # Perform data augmentation if data_augmentation: images, labels = data_augmenter(images, labels, shift=shift, rotate=rotate, scale=scale, intensity=intensity, flip=flip) labels_onehot = convert_to_one_hot(labels).astype(np.float32) labels_onehot = labels_onehot.transpose([1, 0, 2, 3]) labels_onehot = torch.from_numpy(labels_onehot) M = images.copy() M[labels == 0] = 0 # images2 = np.concatenate([images, M], axis=3) # images2 = torch.from_numpy(images.transpose((0, 3, 1, 2))) images = torch.from_numpy(images.transpose((0, 3, 1, 2))) labels = np.expand_dims(labels, axis=1) labels = torch.from_numpy(labels) M = torch.from_numpy(M.transpose((0, 3, 1, 2))) return {'A': images, 'B': labels_onehot, 'M': M}
from utils import convert_to_one_hot from utils import accuracy from utils import random_mini_batches from layers.convolutional_layer import Conv from layers.fullyconnected import FullyConnected from layers.flatten import Flatten from layers.max_pool import MaxPool from activations import relu, lkrelu, linear, sigmoid, cross_entropy from neural_network import Network (X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes) = load_dataset() X_train = X_train_orig/255. X_test = X_test_orig/255. Y_train = convert_to_one_hot(Y_train_orig, 6).T Y_test = convert_to_one_hot(Y_test_orig, 6).T print ("number of training examples = " + str(X_train.shape[0])) print ("number of test examples = " + str(X_test.shape[0])) layers = [ Conv((5, 5, 3, 8), strides=1,pad=2, activation=relu, filter_init=lambda shp: np.random.normal(size=shp) * 1.0 / (5*5*3)), MaxPool(f=8, strides=8, channels = 8), Conv((3, 3, 8, 16), strides=1,pad=1, activation=relu, filter_init=lambda shp: np.random.normal(size=shp) * 1.0 / (3*3*8)), MaxPool(f=4, strides=4, channels = 16), Flatten((2, 2, 16)), FullyConnected((2*2*16, 20), activation=sigmoid, weight_init=lambda shp: np.random.normal(size=shp) * np.sqrt(1.0 / (2*2*16 + 20))), FullyConnected((20, 6), activation=linear, weight_init=lambda shp: np.random.normal(size=shp) * np.sqrt(1.0 / ( 20+ 6))) ] minibatch_size = 20
return G, D_real, D_fake # Define constants NUM_EPOCHS = 100 BATCH_SIZE = 128 LEARNING_RATE = 0.0002 BETA1 = 0.5 NOISE_DIM = 100 NUM_DIGITS = 10 SAMPLE_SIZE = NUM_DIGITS**2 # Load mnist data X_train, Y_train = utils.load_mnist_data() utils.plot_sample(X_train[:SAMPLE_SIZE], "output/mnist_data.png") X_train = utils.preprocess_images(X_train) Y_train = utils.convert_to_one_hot(Y_train) mini_batches = utils.random_mini_batches(X_train, Y_train, BATCH_SIZE) # Create DCGAN X = tf.placeholder(tf.float32, shape=(None, X_train.shape[1], X_train.shape[2], X_train.shape[3])) Y = tf.placeholder(tf.float32, shape=(None, Y_train.shape[1])) Z = tf.placeholder(tf.float32, [None, NOISE_DIM]) G, D_real, D_fake = create_cgan(X, Y, Z) # Create training steps G_loss_func, D_loss_func = utils.create_loss_funcs(D_real, D_fake) G_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="Generator") D_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="Discriminator") G_train_step = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE, beta1=BETA1).minimize(G_loss_func, var_list=G_vars) D_train_step = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE, beta1=BETA1).minimize(D_loss_func, var_list=D_vars)
def preprocess_labels(Y): Y = utils.convert_to_one_hot(Y) Y = np.expand_dims(Y, axis=1) Y = np.expand_dims(Y, axis=1) return Y
def get_epoch_batch(data_list, batch_size, iteration, idx, image_size=192, data_augmentation=False, shift=0.0, rotate=0.0, scale=0.0, intensity=0.0, flip=False, norm=True, aug_rate=0.5): eds, ed_gts, ess, es_gts = [], [], [], [] for i in range(iteration * batch_size, (iteration + 1) * batch_size): es_name, es_gt_name, ed_name, ed_gt_name = data_list[idx[i]] if np.random.uniform() > 0.5: temp = es_name es_name = ed_name ed_name = temp temp = es_gt_name es_gt_name = ed_gt_name ed_gt_name = temp if os.path.exists(es_name) and os.path.exists(es_gt_name): # print(' Select {0} {1}'.format(image_name, label_name)) # Read image and label # print(es_name) es = np.load(es_name) es_gt = np.load(es_gt_name) ed = np.load(ed_name) ed_gt = np.load(ed_gt_name) # Handle exceptions if es.shape != es_gt.shape: print('Error: mismatched size, image.shape = {0}, label.shape = {1}'.format(image.shape, label.shape)) print('Skip {0}, {1}'.format(es_name, es_gt_name)) continue if es.max() < 1e-6: print('Error: blank image, image.max = {0}'.format(es.max())) print('Skip {0} {1}'.format(es_name, es_gt_name)) continue # Append the image slices to the batch # Use list for appending, which is much faster than numpy array Z = es.shape[0] if Z > 8: r = Z - 8 start = random.randint(0, r) for z in range(start, start + 8): temp1 = es[z, :, :] temp2 = ed[z, :, :] if norm: temp1 = rescale_intensity(temp1, (0.5, 99.5)) temp2 = rescale_intensity(temp2, (0.5, 99.5)) ess += [temp1] eds += [temp2] es_gts += [es_gt[z, :, :]] ed_gts += [ed_gt[z, :, :]] else: for z in range(Z): temp1 = es[z, :, :] temp2 = ed[z, :, :] if norm: temp1 = rescale_intensity(temp1, (0.5, 99.5)) temp2 = rescale_intensity(temp2, (0.5, 99.5)) ess += [temp1] eds += [temp2] es_gts += [es_gt[z, :, :]] ed_gts += [ed_gt[z, :, :]] # Convert to a numpy array ESs = np.array(ess, dtype=np.float32) ES_gts = np.array(es_gts, dtype=np.float32) EDs = np.array(eds, dtype=np.float32) ED_gts = np.array(ed_gts, dtype=np.float32) # Add the channel dimension # tensorflow by default assumes NHWC format (batch_size, 128, 128, 1) ESs = np.expand_dims(ESs, axis=3) EDs = np.expand_dims(EDs, axis=3) # Perform data augmentation if data_augmentation and np.random.uniform() > aug_rate: ESs, ES_gts = data_augmenter(ESs, ES_gts, shift=shift, rotate=rotate, scale=scale, intensity=intensity, flip=flip) EDs, ED_gts = data_augmenter(EDs, ED_gts, shift=shift, rotate=rotate, scale=scale, intensity=intensity, flip=flip) ESs_onehot = convert_to_one_hot(ES_gts).astype(np.float32) ESs_onehot = ESs_onehot.transpose([1, 0, 2, 3]) ESs_onehot = torch.from_numpy(ESs_onehot) EDs_onehot = convert_to_one_hot(ED_gts).astype(np.float32) EDs_onehot = EDs_onehot.transpose([1, 0, 2, 3]) EDs_onehot = torch.from_numpy(EDs_onehot) ES_M = ESs.copy() ES_M[ES_gts == 0] = 0 ED_M = EDs.copy() ED_M[ED_gts == 0] = 0 ESs = torch.from_numpy(ESs.transpose((0, 3, 1, 2))) ES_M = torch.from_numpy(ES_M.transpose((0, 3, 1, 2))) EDs = torch.from_numpy(EDs.transpose((0, 3, 1, 2))) ED_M = torch.from_numpy(ED_M.transpose((0, 3, 1, 2))) return {'ED': EDs, 'ED_gt': EDs_onehot,'ED_M': ED_M, 'ES': ESs, 'ES_gt': ESs_onehot,'ES_M': ES_M}
if __name__ == "__main__": num_train = 50000 num_test = 10000 featrue_preserve_radio = .95 x_train, y_train, x_test, y_test = utils.load_data() x_train = x_train[0:num_train, :] y_train = y_train[0:num_train] x_test = x_test[0:num_test, :] y_test = y_test[0:num_test] x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train_pca, x_test_pca = utils.pca(x_train, x_test, featrue_preserve_radio) '''x_train_pca, x_test_pca = utils.pca_with_model(pca_model_name='pca_model.sav', scaler_model_name='scaler_model.sav', x_train=x_train, x_test=x_test)''' y_train = utils.convert_to_one_hot(y_train, 10) y_test = utils.convert_to_one_hot(y_test, 10) model = get_model(x_train_pca.shape[1:], 10) adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) tic = time.time() history = model.fit(x=x_train_pca, y=y_train, epochs=20, batch_size=256, validation_data=(x_test_pca, y_test), callbacks=[TensorBoard(log_dir='./logs')]) toc = time.time() print("train time: " + str(1000 * (toc - tic)) + "ms") utils.plot_history(history) preds = model.evaluate(x_test_pca, y_test) print("Loss = " + str(preds[0])) print("Accuracy = " + str(preds[1]))
def data_prepare_whole_sp(num_class, variable_weight=False, k_spe=10, k_spa=10): #data_prepare # img = scio.loadmat('./Datasets/IndianPines/Indian_pines_corrected.mat')['indian_pines_corrected'] # gt = scio.loadmat('./Datasets/IndianPines/Indian_pines_gt.mat')['indian_pines_gt'] img = scio.loadmat('./Datasets/KSC/KSC.mat')['KSC'] gt = scio.loadmat('./Datasets/KSC/KSC_gt.mat')['KSC_gt'] # img = scio.loadmat('./Datasets/Botswana/Botswana.mat')['Botswana'] # gt = scio.loadmat('./Datasets/Botswana/Botswana_gt.mat')['Botswana_gt'] # img = scio.loadmat('./Datasets/PaviaU/PaviaU.mat')['paviaU'] # gt = scio.loadmat('./Datasets/PaviaU/PaviaU_gt.mat')['paviaU_gt'] # img = scio.loadmat('Datasets/Houston/Houston.mat')['Houston'] # gt = scio.loadmat('Datasets/Houston/Houston_gt.mat')['Houston_gt'] h, w = gt.shape[0], gt.shape[1] c = img.shape[2] idx = np.ones((img.shape[0], img.shape[1])) idx = np.where(idx == 1) idx_x = np.resize(idx[0], (img.shape[0], img.shape[1], 1)) idx_y = np.resize(idx[1], (img.shape[0], img.shape[1], 1)) img_with_idx = np.concatenate((img, idx_x), axis=2) img_with_idx = np.concatenate((img_with_idx, idx_y), axis=2) gt_train = np.zeros((img.shape[0], img.shape[1])) for i in range(1, num_class + 1): id = np.where(gt == i) num = id[0].shape[0] if num >= 50: a = np.ones(num) a = np.where(a == 1) a = list(a[0]) idx_rand = random.sample(a, 40) else: a = np.ones(num) a = np.where(a == 1) a = list(a[0]) idx_rand = random.sample(a, 15) for item in idx_rand: x = id[0][item] y = id[1][item] gt_train[x][y] = i gt_test = gt - gt_train # scio.savemat('./Datasets/Botswana/Botswana_tr_map.mat', {'Botswana_tr_map': gt_train}) # scio.savemat('./Datasets/Botswana/Botswana_te_map.mat', {'Botswana_te_map': gt_test}) gt_train = np.resize(gt_train, (gt_train.shape[0] * gt_train.shape[1])) gt_test = np.resize(gt_test, (gt_test.shape[0] * gt_test.shape[1])) img_with_idx = np.resize(img_with_idx, (h * w, c + 2)) img_train = img_with_idx[gt_train > 0, :] img_test = img_with_idx[gt_test > 0, :] img_whole = np.concatenate((img_train, img_test), axis=0) tr_gt = gt_train[gt_train > 0].astype(int) te_gt = gt_test[gt_test > 0].astype(int) whole_gt = np.concatenate((tr_gt, te_gt), axis=0) # whole_gt = sp.csr_matrix(whole_gt, dtype=np.float32) # img_whole = sp.csr_matrix(img_whole, dtype=np.float32) # img_whole1 = StandardScaler().fit_transform(img_whole) # pca = PCA(n_components=20) # img_whole_pca = pca.fit_transform(img_whole1) s2D_whole_spe, s2D_whole_spa = Mat_dis_s2(img_whole) H_whole_spe = construct_H_with_KNN_from_distance(s2D_whole_spe, k_spe) H_whole_spa = construct_H_with_KNN_from_distance(s2D_whole_spa, k_spa) H_whole = np.concatenate((H_whole_spe, H_whole_spa), axis=1) H_whole = sp.csr_matrix(H_whole, dtype=np.float32) whole_gt = convert_to_one_hot(whole_gt - 1, num_class) whole_gt = whole_gt.T a = tr_gt.shape[0] b = te_gt.shape[0] c = whole_gt.shape[0] mask_TR = sample_mask(np.arange(0, a), whole_gt.shape[0]) mask_TE = sample_mask(np.arange(a, a + b), whole_gt.shape[0]) if variable_weight == False: GHy_whole = _generate_G_from_H(H_whole_spe) img_whole = img_whole[:, :-2] # G_whole = generate_Graph(H_whole_spe) return img_whole, whole_gt, GHy_whole, mask_TR, mask_TE if variable_weight == True: DV2_H, W, invDE_HT_DV2 = _generate_G_from_H( H_whole, variable_weight=variable_weight) #H, W, invDE_HT = _generate_G_from_H(H_whole, variable_weight=variable_weight) img_whole = img_whole[:, :-2] return img_whole, whole_gt, DV2_H, W, invDE_HT_DV2, mask_TR, mask_TE
def evaluate_model(args, model, softmax, criterion, log_loss, dset_loaders, dset_sizes): ''' evaluate per-class accuracy ''' running_loss = 0.0 running_log_loss = 0.0 total_corrects = 0 total_type1 = 0 corrects_type1 = 0 total_type2 = 0 corrects_type2 = 0 total_type3 = 0 corrects_type3 = 0 # switch to evaluate mode model.eval() for data in dset_loaders['val']: # get the inputs if args.mixture: inputs, inputs_seg, labels, _ = data else: inputs, labels, _ = data labels_one_hot = utils.convert_to_one_hot(labels, num_class=3) # wrap them in Variable if args.cuda: inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda()) labels_one_hot = Variable(labels_one_hot.cuda()) if args.mixture: inputs_seg = Variable(inputs_seg.cuda()) else: inputs, labels = Variable(inputs), Variable(labels) labels_one_hot = Variable(labels_one_hot) if args.mixture: inputs_seg = Variable(inputs_seg) # forward outputs = model(inputs) if args.mixture: outputs_seg = model(inputs_seg) if args.mixture: _, preds = torch.max((outputs.data + outputs_seg.data) / 2, dim=1) if args.CE_Loss: loss = criterion((outputs + outputs_seg) / 2, labels) else: loss = criterion(softmax((outputs + outputs_seg) / 2), labels_one_hot) # print(loss_log.size(), loss_log.data[0]) loss_log = log_loss(softmax((outputs + outputs_seg) / 2), labels_one_hot) else: _, preds = torch.max(outputs.data, dim=1) if args.CE_Loss: loss = criterion(outputs, labels) else: loss = criterion(softmax(outputs), labels_one_hot) # print(loss_log.size(), loss_log.data[0]) loss_log = log_loss(softmax(outputs), labels_one_hot) # statistics running_loss += loss.data[0] * inputs.size()[0] running_log_loss += loss_log.data[0] * inputs.size()[0] total_corrects += torch.sum(preds == labels.data) total_type1 += torch.sum(labels.data == 0) corrects_type1 += torch.sum( (preds == labels.data) * (labels.data == 0)) total_type2 += torch.sum(labels.data == 1) corrects_type2 += torch.sum( (preds == labels.data) * (labels.data == 1)) total_type3 += torch.sum(labels.data == 2) corrects_type3 += torch.sum( (preds == labels.data) * (labels.data == 2)) evaluate_loss = running_loss / dset_sizes['val'] evaluate_log_loss = running_log_loss / dset_sizes['val'] evaluate_total_acc = total_corrects / dset_sizes['val'] evaluate_acc_type1 = corrects_type1 / total_type1 evaluate_acc_type2 = corrects_type2 / total_type2 evaluate_acc_type3 = corrects_type3 / total_type3 print('Evaluation results') print('-' * 10) if args.CE_Loss: print('CE_Loss: {:.4f}, Log_loss: {:.4f}, Acc: {:.4f}'.format( evaluate_loss, evaluate_log_loss, evaluate_total_acc)) else: print('BCE_Loss: {:.4f}, Log_loss: {:.4f}, Acc: {:.4f}'.format( evaluate_loss, evaluate_log_loss, evaluate_total_acc)) print('Type1 acc: {:.4f}'.format(evaluate_acc_type1)) print('Type2 acc: {:.4f}'.format(evaluate_acc_type2)) print('Type3 acc: {:.4f}'.format(evaluate_acc_type3))
def train_model(args, model, softmax, criterion, log_loss, optim_scheduler, dset_loaders, dset_sizes, num_epochs, epoch_trained=0, best_epoch_=-1, best_model_logloss_=None, best_model_acc_=0.0, best_model_=None): ''' optim_scheduler: a function which returns an optimizer object when called as optim_scheduler(model, epoch) This is useful when we want to change the learning rate or restrict the parameters we want to optimize. ''' since = time.time() best_model = best_model_ best_model_acc = best_model_acc_ best_model_logloss = best_model_logloss_ best_epoch = best_epoch_ n_batches = { 'train': len(dset_loaders['train']), 'val': len(dset_loaders['val']), 'test': len(dset_loaders['test']) } for epoch in range(epoch_trained + 1, epoch_trained + 1 + num_epochs): print('Epoch {}/{}'.format(epoch, epoch_trained + num_epochs)) print('-' * 10) val_acc = 0.0 val_logloss = None # Each epoch has a training and validation phase for phase in ['train', 'val', 'test']: if phase == 'train': model.train() optimizer, lr = optim_scheduler( model, epoch, optimizer_name=args.optimizer, init_lr=args.lr, slow_base=args.slow_base, lr_decay_factor=args.lr_decay_factor, lr_decay_epoch=args.lr_decay_epoch, momentum=args.momentum, weight_decay=args.weight_decay, warmup=args.warmup, warm_lr=args.warm_lr, warm_epochs=args.warm_epochs, warmup_type=args.warmup_type, cos_schedule=bool(args.cos_lr), cos_schedule_params={ 'T': num_epochs, 'M': args.M, 'init_lr': args.lr }) log_value('lr', lr, step=epoch) elif phase == 'val' or phase == 'test': model.eval() running_loss = 0.0 running_log_loss = 0.0 running_corrects = 0 # Iterate over data. step = 1 for data in tqdm(dset_loaders[phase]): # get the inputs if args.mixture: inputs, inputs_seg, labels, _ = data mix_w = 0.5 else: inputs, labels, _ = data labels_one_hot = utils.convert_to_one_hot(labels, num_class=3) # wrap them in Variable if args.cuda: inputs, labels = Variable(inputs.cuda()), Variable( labels.cuda()) labels_one_hot = Variable(labels_one_hot.cuda()) else: inputs, labels = Variable(inputs), Variable(labels) labels_one_hot = Variable(labels_one_hot) # zero the parameter gradients optimizer.zero_grad() # forward ===================================================================== if 'inception_v3' in args.arch and 'stn' in args.arch and phase == 'train': outputs, stn_aux, incep_aux = model(inputs) elif 'inception_v3' in args.arch and 'stn' not in args.arch and phase == 'train': outputs, incep_aux = model(inputs) elif 'inception_v3' not in args.arch and 'stn' in args.arch and phase == 'train': outputs, stn_aux = model(inputs) else: outputs = model(inputs) # mixture mode # CATION: stn with mixture mode is incompleted if args.mixture: inputs_seg = Variable(inputs_seg.cuda( )) if args.cuda else Variable(inputs_seg) if 'inception_v3' in args.arch and phase == 'train': outputs_seg, aux_outputs_seg = model(inputs_seg) else: outputs_seg = model(inputs_seg) if args.mixture: # CATION: stn with mixture mode is incompleted _, preds = torch.max(outputs.data * (1 - mix_w) + outputs_seg.data * mix_w, dim=1) loss = criterion( outputs * (1 - mix_w) + outputs_seg * mix_w, labels) if 'inception_v3' in args.arch and phase == 'train': incep_auxloss = criterion( incep_aux * (1 - mix_w) + aux_outputs_seg * mix_w, labels) loss_log = log_loss( softmax(outputs * (1 - mix_w) + outputs_seg * mix_w), labels_one_hot) else: _, preds = torch.max(outputs.data, dim=1) loss = criterion(outputs, labels) if 'inception_v3' in args.arch and phase == 'train': incep_auxloss = criterion(incep_aux, labels) loss_log = log_loss(softmax(outputs), labels_one_hot) # backward + optimize only if in training phase ===================================== if phase == 'train': if 'inception_v3' in args.arch and 'stn' in args.arch: stn_auxloss = torch.mean(stn_aux) total_loss = loss + 0.1 * stn_auxloss + incep_auxloss total_loss.backward() elif 'inception_v3' not in args.arch and 'stn' in args.arch: stn_auxloss = torch.mean(stn_aux) total_loss = loss + 0.1 * stn_auxloss total_loss.backward() elif 'inception_v3' in args.arch and 'stn' not in args.arch: total_loss = loss + incep_auxloss total_loss.backward() else: loss.backward() optimizer.step() # statistics running_loss += loss.data[0] * inputs.size()[0] running_log_loss += loss_log.data[0] * inputs.size()[0] running_corrects += torch.sum(preds == labels.data) global_step = (epoch - 1) * n_batches[phase] + step log_value(phase + '_{}'.format(args.loss), loss.data[0], step=global_step) log_value(phase + '_log_loss', loss_log.data[0], step=global_step) log_value(phase + '_acc', torch.mean((preds == labels.data).type_as( torch.FloatTensor())), step=global_step) step += 1 # free the graph to avoid memory increase del outputs, loss, loss_log if args.mixture: del outputs_seg epoch_loss = running_loss / dset_sizes[phase] epoch_log_loss = running_log_loss / dset_sizes[phase] epoch_acc = running_corrects / dset_sizes[phase] print('{} {}_Loss: {:.4f}, Log_loss: {:.4f}, Acc: {:.4f}'.format( phase, loss_name, epoch_loss, epoch_log_loss, epoch_acc)) log_value('epoch{}_{}'.format(phase, args.loss), epoch_loss, step=epoch) log_value('epoch{}_log_loss'.format(phase), epoch_log_loss, step=epoch) log_value('epoch{}_acc'.format(phase), epoch_acc, step=epoch) if phase == 'val': val_acc = epoch_acc val_logloss = epoch_log_loss # deep copy the model if phase == 'val' and (best_model_logloss is None or epoch_log_loss < best_model_logloss): best_model_acc = epoch_acc best_model_logloss = epoch_log_loss best_epoch = epoch best_model = copy.deepcopy(model) # do checkpointing if epoch % args.ckpt_epoch == 0: save_checkpoint(state={ 'epoch': epoch, 'val_logloss': val_logloss, 'val_acc': val_acc, 'state_dict': model.state_dict() }, save_path='{0}/model_epoch_{1}.pth'.format( args.ckpt_dir, epoch)) save_checkpoint(state={ 'epoch': best_epoch, 'val_logloss': best_model_logloss, 'val_acc': best_model_acc, 'state_dict': best_model.state_dict() }, save_path='{0}/best_model.pth'.format( args.ckpt_dir)) print() time_elapsed = time.time() - since print('Training complete in {:.0f}m {:.0f}s'.format( time_elapsed // 60, time_elapsed % 60)) print('Best val Acc: {:4f}, best epoch: {}'.format(best_model_acc, best_epoch)) return best_model