Graduate School/Neural Network
Image Prediction
- -
728x90
반응형
Import Library¶
In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
2022-11-23 17:10:00.971077: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Load Pre-Trained Model (ResNet50)¶
In [2]:
model = tf.keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet') # still doing classification using pre-trained weight
2022-11-23 17:10:03.415147: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-11-23 17:10:03.920049: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 22103 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:65:00.0, compute capability: 8.6
In [3]:
len(model.layers)
Out[3]:
177
In [4]:
model.layers
Out[4]:
[<keras.engine.input_layer.InputLayer at 0x7f26bf420820>, <keras.layers.reshaping.zero_padding2d.ZeroPadding2D at 0x7f26bf420f40>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bfa9a0a0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26beba86a0>, <keras.layers.core.activation.Activation at 0x7f27383b1be0>, <keras.layers.reshaping.zero_padding2d.ZeroPadding2D at 0x7f26beba8520>, <keras.layers.pooling.max_pooling2d.MaxPooling2D at 0x7f26bc1fd4f0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1fd160>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc2158b0>, <keras.layers.core.activation.Activation at 0x7f26bc215880>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc21a850>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc214c70>, <keras.layers.core.activation.Activation at 0x7f26bc214c40>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1fdd60>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc20d820>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1fd8e0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc22bb20>, <keras.layers.merging.add.Add at 0x7f26bc2079a0>, <keras.layers.core.activation.Activation at 0x7f26bc21ab80>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc2339d0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1bae50>, <keras.layers.core.activation.Activation at 0x7f26bc1bab80>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc226940>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1bdee0>, <keras.layers.core.activation.Activation at 0x7f26bc22b0d0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1d2a60>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1d6e50>, <keras.layers.merging.add.Add at 0x7f26bc233df0>, <keras.layers.core.activation.Activation at 0x7f26bc1d2550>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1defa0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1e3a30>, <keras.layers.core.activation.Activation at 0x7f26bc1c5430>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1eeee0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1f13d0>, <keras.layers.core.activation.Activation at 0x7f26bc1d21c0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc17bca0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc17d7f0>, <keras.layers.merging.add.Add at 0x7f26bc17b040>, <keras.layers.core.activation.Activation at 0x7f26bc1e3dc0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1de850>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1e33a0>, <keras.layers.core.activation.Activation at 0x7f26bc21afa0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1fd4c0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc188e20>, <keras.layers.core.activation.Activation at 0x7f26bc1fd790>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1d6160>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc18edc0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc21a1c0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26beba8d90>, <keras.layers.merging.add.Add at 0x7f26bc194be0>, <keras.layers.core.activation.Activation at 0x7f26bc194a30>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1ca670>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc19bf70>, <keras.layers.core.activation.Activation at 0x7f26bc191c70>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1a3550>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1aa7f0>, <keras.layers.core.activation.Activation at 0x7f26bc1aa6a0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1af400>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1b66a0>, <keras.layers.merging.add.Add at 0x7f26bc194ee0>, <keras.layers.core.activation.Activation at 0x7f26bc1a36d0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc143b50>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc14a7c0>, <keras.layers.core.activation.Activation at 0x7f26bc152e50>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc152c40>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc152910>, <keras.layers.core.activation.Activation at 0x7f26bc14a2e0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1431f0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1678b0>, <keras.layers.merging.add.Add at 0x7f26bc160e50>, <keras.layers.core.activation.Activation at 0x7f26bc16e0a0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0f7310>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0fd190>, <keras.layers.core.activation.Activation at 0x7f26bc15cfd0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0fd580>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc104c40>, <keras.layers.core.activation.Activation at 0x7f26bc1081c0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0fd9a0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0f7a00>, <keras.layers.merging.add.Add at 0x7f26bc1086a0>, <keras.layers.core.activation.Activation at 0x7f26bc1432e0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1675e0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1ca4f0>, <keras.layers.core.activation.Activation at 0x7f26bc160d90>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc11b160>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc11b190>, <keras.layers.core.activation.Activation at 0x7f26bc1203a0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1b63a0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1208e0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1aafd0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc11baf0>, <keras.layers.merging.add.Add at 0x7f26bc127580>, <keras.layers.core.activation.Activation at 0x7f26bc11bac0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1207c0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc12c9d0>, <keras.layers.core.activation.Activation at 0x7f26bc124640>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1350a0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc1350d0>, <keras.layers.core.activation.Activation at 0x7f26bc12c2b0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0c3e50>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0c9850>, <keras.layers.merging.add.Add at 0x7f26bc0c3fa0>, <keras.layers.core.activation.Activation at 0x7f26bc0c9c10>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0d72b0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0d7070>, <keras.layers.core.activation.Activation at 0x7f26bc0bc1f0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0df640>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0e4b80>, <keras.layers.core.activation.Activation at 0x7f26bc0df6a0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0ea190>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0f2ac0>, <keras.layers.merging.add.Add at 0x7f26bc0e48b0>, <keras.layers.core.activation.Activation at 0x7f26bc0809d0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc07b9d0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc08ab50>, <keras.layers.core.activation.Activation at 0x7f26bc090dc0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc090400>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0f2760>, <keras.layers.core.activation.Activation at 0x7f26bc0904f0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0cff10>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0cf8e0>, <keras.layers.merging.add.Add at 0x7f26bc12c4f0>, <keras.layers.core.activation.Activation at 0x7f26bc120340>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc1ca7c0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc152b80>, <keras.layers.core.activation.Activation at 0x7f26bc124790>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc097fa0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc097e80>, <keras.layers.core.activation.Activation at 0x7f26bc1fdc40>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc09ef10>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0a27c0>, <keras.layers.merging.add.Add at 0x7f26bc09efa0>, <keras.layers.core.activation.Activation at 0x7f26bc0a2880>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc09cdf0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0a8160>, <keras.layers.core.activation.Activation at 0x7f26bc09c640>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0b0880>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0b6a90>, <keras.layers.core.activation.Activation at 0x7f26bc0b07c0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0b6850>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0429a0>, <keras.layers.merging.add.Add at 0x7f26bc0b6730>, <keras.layers.core.activation.Activation at 0x7f26bc050370>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc057940>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc057430>, <keras.layers.core.activation.Activation at 0x7f26bc120760>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc06edf0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc076730>, <keras.layers.core.activation.Activation at 0x7f26bc067f40>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc04a1c0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26b07c4790>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc042e50>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc06e3a0>, <keras.layers.merging.add.Add at 0x7f26b07d2eb0>, <keras.layers.core.activation.Activation at 0x7f26b07d2700>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26b07c4ac0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0670d0>, <keras.layers.core.activation.Activation at 0x7f26b07caa00>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc067c40>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26b07d2100>, <keras.layers.core.activation.Activation at 0x7f26bc06ec10>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0a8850>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc188fa0>, <keras.layers.merging.add.Add at 0x7f26bc0a4c10>, <keras.layers.core.activation.Activation at 0x7f26bc0f2af0>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26bc0d76a0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26bc0d7370>, <keras.layers.core.activation.Activation at 0x7f26bc0bc250>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26b07de9a0>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26b07d7b20>, <keras.layers.core.activation.Activation at 0x7f26b07d7f10>, <keras.layers.convolutional.conv2d.Conv2D at 0x7f26b07e8070>, <keras.layers.normalization.batch_normalization.BatchNormalization at 0x7f26b07e2790>, <keras.layers.merging.add.Add at 0x7f26bc0c9970>, <keras.layers.core.activation.Activation at 0x7f26b07d7dc0>, <keras.layers.pooling.global_average_pooling2d.GlobalAveragePooling2D at 0x7f26b07c4850>, <keras.layers.core.dense.Dense at 0x7f26b07f7dc0>]
In [5]:
model.layers[0].input_shape
Out[5]:
[(None, 224, 224, 3)]
In [6]:
model.layers[-1].output_shape
Out[6]:
(None, 1000)
Load Image¶
In [7]:
image_file = 'cat.jpg'
im = plt.imread(image_file)
In [8]:
plt.imshow(im)
print(im.max())
print(im.min())
252 14
Image Pre-Processing¶
In [9]:
preprocess = tf.keras.applications.imagenet_utils.preprocess_input
im_processed = preprocess(im)
In [10]:
plt.imshow(im_processed)
print(im_processed.max())
print(im_processed.min())
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
146.061 -102.779
Image Prediction¶
In [11]:
im_input = im_processed[np.newaxis, :, :, :]
res = model.predict(im_input)
class_id = np.argmax(res)
2022-11-23 17:10:08.847055: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8401
1/1 [==============================] - 6s 6s/step
2022-11-23 17:10:12.028093: I tensorflow/stream_executor/cuda/cuda_blas.cc:1786] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.
In [12]:
print(res.shape)
print(class_id)
(1, 1000) 282
Using Custom Activation Function¶
In [13]:
@tf.custom_gradient
def custom_relu(x):
def grad(dy):
return tf.cast(x>0, tf.float32)*tf.cast(dy>0, tf.float32)*dy
return tf.nn.relu(x), grad
cnt = 0
for layer in model.layers:
if hasattr(layer, 'activation'):
if layer.activation == tf.keras.activations.relu:
layer.activation = custom_relu
cnt += 1
In [14]:
cnt
Out[14]:
49
Get Gradient¶
In [15]:
im_tensor = tf.Variable(im_input)
with tf.GradientTape() as tape:
scores = model(im_tensor)
max_score = tf.reduce_max(scores)
grads = tape.gradient(max_score, im_tensor)
In [16]:
grads = grads.numpy()
In [17]:
grads.shape
Out[17]:
(1, 224, 224, 3)
In [18]:
grads = np.squeeze(grads)
In [19]:
plt.imshow(grads)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Out[19]:
<matplotlib.image.AxesImage at 0x7f269c3aa490>
In [20]:
plt.hist(grads.flatten())
Out[20]:
(array([5.00000e+00, 2.10000e+01, 4.90000e+01, 2.03000e+02, 1.91200e+03, 1.47289e+05, 9.13000e+02, 1.09000e+02, 2.20000e+01, 5.00000e+00]), array([-0.0025214 , -0.00205292, -0.00158444, -0.00111595, -0.00064747, -0.00017899, 0.00028949, 0.00075797, 0.00122645, 0.00169493, 0.00216341], dtype=float32), <BarContainer object of 10 artists>)
In [21]:
g = (grads-grads.min())/(grads.max()-grads.min())
In [22]:
plt.hist(g.flatten())
Out[22]:
(array([5.00000e+00, 2.10000e+01, 4.90000e+01, 2.03000e+02, 1.91200e+03, 1.47289e+05, 9.13000e+02, 1.09000e+02, 2.20000e+01, 5.00000e+00]), array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ], dtype=float32), <BarContainer object of 10 artists>)
In [23]:
plt.imshow(g)
Out[23]:
<matplotlib.image.AxesImage at 0x7f269c20f250>
In [ ]:
728x90
반응형
'Graduate School > Neural Network' 카테고리의 다른 글
LSTM을 이용한 주식 가격 예측 (6) | 2024.09.10 |
---|---|
Face Recognition (0) | 2024.09.10 |
Linear Classifier 02 (0) | 2024.09.10 |
Linear Classifier 01 (0) | 2024.09.10 |
Contents
소중한 공감 감사합니다