Complete CAM based on cifar10 data set

preface

In the previous blog, we simply completed the pseudo code of CAM algorithm, address:
https://blog.csdn.net/qq_43534932/article/details/107425740
Without real operation, we use the small data set cifar10 to complete the realization of the basic functions of CAM.
cifar10 data set: it is a data set collected by CIFAR(Candian Institute For Advanced Research) for machine learning and image recognition. This data set contains 60000 32 * 32 color images covering 10 categories. The specific classification is shown in the figure below:

Classification model

In the previous blog, we also introduced CAM. CAM adopts the scheme of transfer learning. First, we need to train a classification model

image_holder = tf.placeholder(tf.float32, [None, 24, 24, 3])
label_holder = tf.placeholder(tf.int32, [None])


layer1=tf.layers.conv2d(image_holder,64,5,strides=1, padding='same')
layer1=tf.nn.relu(layer1)

layer1=model.Resblock(layer1,3,1,32)
#layer1=model.SK_block(layer1,3,5,128)
layer1=model.Resblock(layer1,5,1,32)
#layer1=tf.nn.max_pool(layer1, 5, 2, padding="SAME")
layer1=model.Resblock(layer1,3,1,32)
#layer1=tf.nn.max_pool(layer1, 5, 2, padding="SAME")
layer1=model.Resblock(layer1,3,1,32)
#layer1=tf.nn.max_pool(layer1, 5, 2, padding="SAME")
layer1=model.Resblock(layer1,3,1,32)
layer1=model.Resblock(layer1,3,1,32)

###########classification

layer1_transpose=tf.layers.conv2d_transpose(layer1,10,3,strides=1, padding='same')
layer1_transpose=tf.nn.relu(layer1_transpose)
print(layer1_transpose)

GAP=tf.keras.layers.GlobalAvgPool2D()(layer1_transpose)
print(GAP)

# The number of nodes in the full connection layer and hidden layer has been reduced by half


'''The standard deviation of normal distribution is set as the reciprocal of the number of nodes in the previous hidden layer and is not included L2 Regularity of'''
weight5 = variable_with_weight_loss(shape=[10, 10], stddev=1 / 10, w1=0.0)
bias5 = tf.Variable(tf.constant(0.0, shape=[10]))
logits = tf.add(tf.matmul(GAP, weight5), bias5)

Based on the cifair dataset, we did not downsampling as in the article, because we found that the recovery result after downsampling is very problematic.
Here we write a model module, which includes Resblock, SKblock and so on.
Based on this, because of the relationship between computing power and time, we trained a model with an accuracy of about 80%.

CAM

The CAM algorithm is similar to the pseudo code reproduced before, but the model reading function should be added to read out the corresponding layer layer for calculation and complete the CAM

data_dir = './cifar10_data/cifar-10-batches-bin'
images_test, labels_test = cifar10_input.inputs(eval_data=True,
                                                data_dir=data_dir,
                                                batch_size=1)
print(images_test,labels_test)


saver = tf.train.import_meta_graph( './Mobel_service/best.ckpt.meta')# Load graph structure
gragh = tf.get_default_graph()# Get the current graph to recover variables during subsequent training
tensor_name_list = [tensor.name for tensor in gragh.as_graph_def().node]# Get the names of all variables in the current graph
#print(tensor_name_list)
x = gragh.get_tensor_by_name('Placeholder:0')
y = gragh.get_tensor_by_name('Placeholder_1:0')
feature = gragh.get_tensor_by_name('Relu_20:0')
weights = gragh.get_tensor_by_name('global_average_pooling2d_1/Mean:0')
CAM = tf.reduce_sum(feature*weights, -1)
print(CAM)



sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
tf.train.start_queue_runners()

image_batch, label_batch = sess.run([images_test, labels_test])
CAM_result=sess.run([CAM], feed_dict={x: image_batch, y: label_batch})
CAM_result = np.reshape(CAM_result, [24,24])

print(CAM_result.shape)
###display
image_batch = np.reshape(image_batch, [24,24,3])
plt.figure()
plt.subplot(1,2,1)
plt.imshow(image_batch, interpolation = 'bilinear')
plt.subplot(1,2,2)
plt.imshow(CAM_result, interpolation = 'bilinear')
plt.show()
#####
print(image_batch.shape)
print(label_batch.shape)

Result display


Leaving aside the fraction of our picture reading, we can see that it can still show certain CAM advantages. I will upload the complete code.

Tags: AI

Posted by brownca on Fri, 13 May 2022 11:57:46 +0300