|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Tue Jan 14 11:32:37 2020 |
| 4 | +
|
| 5 | +@author: pku |
| 6 | +""" |
| 7 | +#%% |
| 8 | +import os |
| 9 | +import numpy as np |
| 10 | +from PIL import Image |
| 11 | +from tensorflow.keras import applications |
| 12 | +from tensorflow.keras import optimizers |
| 13 | +from tensorflow.keras.models import Sequential, Model |
| 14 | +from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping |
| 15 | +from tensorflow.keras.layers import Flatten, Dense, Dropout |
| 16 | + |
| 17 | +import matplotlib.pyplot as plt |
| 18 | +import h5py |
| 19 | +#%% |
| 20 | + |
| 21 | + |
| 22 | +nb_train_samples = 16000 |
| 23 | +nb_validation_samples = 500 |
| 24 | + |
| 25 | + |
| 26 | +#%% |
| 27 | + |
| 28 | +data = np.load('../Rsp.npy') |
| 29 | +test = np.load('../valRsp.npy') |
| 30 | + |
| 31 | + |
| 32 | +n = nb_train_samples |
| 33 | +train_x1 = np.load('../train_r132.npy') |
| 34 | +val_x1 = np.load('../val_r132.npy') |
| 35 | + |
| 36 | +#%% |
| 37 | + |
| 38 | +n1 = 11*11*1024 |
| 39 | +STD1 = np.std(train_x1) |
| 40 | + |
| 41 | + |
| 42 | +train_x = np.reshape(train_x1,(n,n1))/STD1 |
| 43 | +val_x = np.reshape(val_x1,(nb_validation_samples,n1))/STD1 |
| 44 | + |
| 45 | + |
| 46 | +train_y = np.reshape(data,(n,128*128)) |
| 47 | +val_y = np.reshape(test,(nb_validation_samples,128*128)) |
| 48 | +val_y1 = np.reshape(val_y,(nb_validation_samples,128,128,1)) |
| 49 | + |
| 50 | + |
| 51 | +fnum = 128*128 |
| 52 | +ROI = np.mean(test**2,0)>0 |
| 53 | +roi = np.reshape(ROI,(fnum)) |
| 54 | +Cnum = 200 |
| 55 | + |
| 56 | +L1 = 1e-4 |
| 57 | +#%% |
| 58 | +from tensorflow.keras.layers import Conv2D,BatchNormalization, MaxPooling2D, DepthwiseConv2D, Activation, GaussianNoise, LocallyConnected1D, Reshape, Conv2DTranspose |
| 59 | +from tensorflow.keras import regularizers |
| 60 | +from tensorflow.keras import activations |
| 61 | +from tensorflow.keras.models import load_model |
| 62 | +from tensorflow.keras import backend as K |
| 63 | +import tensorflow as tf |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +def mean_squared_error_noise(y_true, y_pred): |
| 68 | + return K.mean(K.square(K.relu(K.abs(y_pred - y_true)-0.0)), axis=-1) |
| 69 | + |
| 70 | +# to deal with Failed to get convolution algorithm. |
| 71 | +config = tf.compat.v1.ConfigProto() |
| 72 | +config.gpu_options.allow_growth = True |
| 73 | +session = tf.compat.v1.InteractiveSession(config=config) |
| 74 | + |
| 75 | +#alexnet = load_model('alexnet.h5') |
| 76 | +model = Sequential() |
| 77 | +model.add(Dropout(0.1,input_shape=(n1,))) |
| 78 | +model.add(Dense(Cnum, kernel_regularizer=regularizers.l1(L1/Cnum), activation='elu')) |
| 79 | +model.add(Dense(fnum, use_bias=True)) |
| 80 | + |
| 81 | + |
| 82 | +w1 = model.layers[-1].get_weights() |
| 83 | +w2 = np.zeros((Cnum, fnum)) |
| 84 | +w3 = np.zeros((fnum)) |
| 85 | +w3[roi] = 0.001 |
| 86 | +for i in range(Cnum): |
| 87 | + w2[i,:] = w3 |
| 88 | + |
| 89 | +w1[0] = w2 |
| 90 | +model.layers[-1].set_weights(w1) |
| 91 | + |
| 92 | + |
| 93 | +#adadelta=optimizers.Adadelta(lr=0.001, rho=0.95, epsilon=1e-06) |
| 94 | +Adam = optimizers.Adam(lr=0.00002, beta_1=0.9, beta_2=0.999, epsilon=1e-08) |
| 95 | +#model.layers[1].kernel_regularizer=regularizers.l1(1e-2/Cnum) |
| 96 | + |
| 97 | +model.compile(loss=mean_squared_error_noise, optimizer=Adam, metrics=['mse']) |
| 98 | +model.summary() |
| 99 | +model.save_weights('initial.hdf5') |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +#%% |
| 104 | + |
| 105 | +filepath="Cell.hdf5" |
| 106 | +model.load_weights('initial.hdf5') |
| 107 | + |
| 108 | +#earlyStopping=EarlyStopping(monitor='val_mse', patience=30, verbose=1, mode='auto') |
| 109 | +saveBestModel = ModelCheckpoint(filepath, monitor='val_mse', verbose=1, save_best_only=True, mode='auto') |
| 110 | +#callbacks_list = [earlyStopping,saveBestModel] |
| 111 | +callbacks_list = [saveBestModel] |
| 112 | + |
| 113 | + |
| 114 | +model.fit(train_x, train_y, epochs=20, batch_size=20, validation_data=(val_x, val_y), callbacks=callbacks_list) |
| 115 | + |
| 116 | +#%% test trained model |
| 117 | +model.load_weights(filepath) |
| 118 | +pred = model.predict(val_x) |
| 119 | +pred1 = np.reshape(pred,(nb_validation_samples,128,128,1)) |
| 120 | + |
| 121 | +R = np.zeros((128,128)) |
| 122 | +VE = np.zeros((128,128)) |
| 123 | +for i in range(128): |
| 124 | + for j in range(128): |
| 125 | + if np.sum(np.abs(val_y1[:,i,j,0]))>0: |
| 126 | + |
| 127 | + u2=np.zeros((2,nb_validation_samples)) |
| 128 | + u2[0,:]=np.reshape(pred1[:,i,j,0],(nb_validation_samples)) |
| 129 | + u2[1,:]=np.reshape(val_y1[:,i,j,0],(nb_validation_samples)) |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + c2=np.corrcoef(u2) |
| 134 | + R[i,j] = c2[0,1] |
| 135 | + VE[i,j] = 1-np.var(pred1[:,i,j,0]-val_y1[:,i,j,0])/np.var(val_y1[:,i,j,0]) |
| 136 | + |
| 137 | + |
| 138 | +np.save('R.npy',R) |
| 139 | +np.save('VE.npy',VE) |
| 140 | + |
| 141 | +print('L1:',L1) |
| 142 | +print('R:',np.mean(R[ROI])) |
| 143 | + |
| 144 | +K.clear_session() |
| 145 | + |
| 146 | + |
0 commit comments