Visualization of Keras models, layer visualization and kernel visualization

Source: Internet
Author: User
Tags keras

Visualization of Keras Models:

Model

Model = sequential () # INPUT:100X100 images with 3 channels, (3) tensors.
# This applies, convolution filters of size 3x3 each. Model.add (Zeropadding2d (1), Input_shape= (3, 3)) Model.add (conv2d (+)' Relu ', padding=' Same ') # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ')) Model.add (Batchnormalization ()) Model.add ( Maxpooling2d (Pool_size= (2, 2)) Model.add (Dropout (0.25)) Model.add (conv2d (3, 3), activation=' Relu ', padding=' Same ',)) # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ',)) Model.add (Batchnormalization ()) Model.add ( Maxpooling2d (Pool_size= (2, 2)) Model.add (Dropout (0.25)) Model.add (conv2d (+ 3, 3), activation=' Relu ', padding=' Same ',)) # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ',)) Model.add (Batchnormalization ()) Model.add ( Maxpooling2d (Pool_size= (2, 2)) Model.add (Dropout (0.25)) Model.add (Averagepooling2d ((5,5))) Model.add (Flatten ()) # Model.add (Dense (activation= ' Relu ')) # Model.add (Dropout (0.5)) Model.add (Dense (label_size, activation=' Softmax '))

1. Layer Visualization:

test_x = [] img_src = Cv2.imdecode (Np.fromfile (R ' c:\temp.tif ', dtype=np.uint8), Cv2. Imread_grayscale) img = cv2.resize (IMG_SRC, (up to), Interpolation=cv2. INTER_CUBIC) # img = Np.random.randint (0,255, (38,38)) img = (255-img)/255 img = Np.reshape (IMG, 1)) Test_x.ap Pend (img) ################################################################### layer = model.layers[1] Weight =  Layer.get_weights () # Print (weight) print (Np.asarray (weight). Shape) MODEL_V1 = Sequential () # input:100x100 images with 3
Channels, (3), tensors.
# This applies, convolution filters of size 3x3 each. Model_v1.add (Zeropadding2d (1, 1), input_shape= (1)) Model_v1.add (conv2d (+ 3, 3), activation=' Relu ', padding=' Same ') # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ')) model_v1.layers[1].set_weights (weight) re = Model_ V1.predict (Np.array (test_x)) print (Np.shape (re)) re = Np.transpose (Re, (0,3,1,2)) forIinchRange (+): Plt.subplot (4,8,i+1) plt.imshow (Re[0][i]) #, cmap= ' Gray ' plt.show () ################################## ################################ MODEL_V2 = sequential () # INPUT:100X100 images with 3 channels (3) Tenso
Rs.
# This applies, convolution filters of size 3x3 each. Model_v2.add (Zeropadding2d (1, 1), input_shape= (1)) Model_v2.add (conv2d (+ 3, 3), activation=' Relu ', padding=' Same ') # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ')) Model_v2.add (Batchnormalization ()) Model_v2.add ( Maxpooling2d (Pool_size= (2, 2)) Model_v2.add (Dropout (0.25)) Model_v2.add (conv2d (3, 3), activation=' Relu ', padding=' Same ',)) print (Len (model_v2.layers)) Layer1 = model.layers[1] weight1 = layer1.get_weights () model_v2.layers[1].set_weights (weight1) Layer5 = model.layers[5] Weight5 = Layer5.get_weights () model_v2.layers[5].set_weights (weight5) Re2 = Model_v2 . Predict (Np.array (test_x)) Re2 = Np.transpose (Re2, (0,3,1,2)) forIinchRange: Plt.subplot (8,8,i+1) plt.imshow (Re2[0][i]) #, cmap= ' Gray ' plt.show () ################################# ################################# model_v3 = sequential () # INPUT:100X100 images with 3 channels (3) Tens
Ors.
# This applies, convolution filters of size 3x3 each. Model_v3.add (Zeropadding2d (1, 1), input_shape= (1)) Model_v3.add (conv2d (+ 3, 3), activation=' Relu ', padding=' Same ') # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ')) Model_v3.add (Batchnormalization ()) Model_v3.add ( Maxpooling2d (Pool_size= (2, 2)) Model_v3.add (Dropout (0.25)) Model_v3.add (conv2d (3, 3), activation=' Relu ', padding=' Same ',)) # Model.add (conv2d (3, 3), activation= ' Relu ', padding= ' same ',)) Model_v3.add (Batchnormalization ()) Model_ V3.add (Maxpooling2d (pool_size= (2, 2)) Model_v3.add (Dropout (0.25)) Model_v3.add (conv2d (+ 3, 3), activation=' Relu ', padding=' Same ',)) print (Len (model_v3.layers)) Layer1 = model.layers[1] weight1 = layer1.get_weights () model_v3.layers[1].set_weights (weight1) Layer5 = model.layers[5] Weight5 = Layer5.get_weights () model_v3.layers[5].set_weights (WEIGHT5) Layer9 = model . layers[9] Weight9 = Layer9.get_weights () model_v3.layers[9].set_weights (weight9) Re3 = Model_v3.predict (Np.array ( test_x)) Re3 = Np.transpose (Re3, (0,3,1,2)) forIinchRange (121): Plt.subplot (11,11,i+1) plt.imshow (Re3[0][i]) #, cmap= ' Gray ' plt.show ()



2.kernel Visualization:

defProcess (x): res = np.clip (x, 0, 1)returnResdefDprocessed (x): res = np.zeros_like (x) Res + = 1 Res[x < 0] = 0 res[x > 1] = 0returnResdefDeprocess_image (x): X-= X.mean () x/= (X.STD () + 1e-5) x *= 0.1 x + = 0.5 x = np.clip (x, 0, 1) x * = 255 x = np.clip (x, 0, 255). Astype (' Uint8 ')returnX forI_kernalinchRange: Input_img=model.input loss = K.mean (model.layers[5].output[:,:,:, I_kernal]) # loss = K.mean (model. output[:, i_kernal]) # Compute the gradient of the input picture wrt this loss grads = K.gradients (loss, input_img  ) [0] # Normalization Trick:we normalize the gradient grads/= (k.sqrt (K.mean (k.square)) + 1e-5) # this function returns the loss and grads given the input picture iterate = K.function ([Input_img, K.learning_phase ()], [Lo SS, grads]) # We start from a gray image with some noise np.random.seed (0) Num_channels=1 img_height=img_w
    idth=38 Input_img_data = (255-np.random.randint (0,255, (1, Img_height, Img_width, num_channels))/255. Failed =False # Run Gradient ascent print ('####################################', i_kernal+1) loss_value_pre=0 forIinchRange (10000): # processed = Process (input_img_data) # predictions = Model.predict (input_img_data) Loss_value, Grads_value = Iterate ([input_img_data,1]) # Grads_value *= dprocessed (input_img_data[0])ifi%1000 = = 0: # print (' predictions: ', np.shape (predictions), Np.argmax (predictions)) print (' Iteration%d/%d, loss:%f '% (I, 10000, loss_value)) print (' Mean Grad:%f '% Np.mean (grads_value))ifAll (Np.abs (Grads_val) < 0.000001 forGrads_valinchGrads_value.flatten ()): failed =True Print' Failed ') Break # print (' image:\n%s '% str (input_img_data[0,0,:,:]))ifLoss_value_pre! = 0 andLoss_value_pre > Loss_value: BreakifLoss_value_pre = = 0:loss_value_pre = loss_value # if Loss_value > 0.99: # Break Input_img_data + = Grads_value * 1 #e-3 Plt.subplot (8, 8, i_kernal+1) # plt.imshow (Process (input_i mg_data[0,:,:,0]) *255). Astype (' uint8 '), cmap= ' greys ') #cmap = ' Greys ' img_re = Deprocess_image (input_img_data[0]) im G_re = Np.reshape (Img_re, (38,38)) plt.imshow (Img_re, cmap=' Greys ') #cmap = ' Greys ' # plt.show () plt.show ()

MODEL.LAYERS[1]

MODEL.LAYERS[5]

MODEL.LAYERS[-1]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.