Skip to content

Commit 3c098ba

Browse files
committed
The proposed depthwise convolutional layer and constraints
1 parent a5b08fe commit 3c098ba

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"name": "Sub-ImageNet-ResNet50-Depthw-constraints-224.ipynb",
7+
"provenance": [],
8+
"collapsed_sections": [],
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"accelerator": "GPU"
16+
},
17+
"cells": [
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {
21+
"id": "view-in-github",
22+
"colab_type": "text"
23+
},
24+
"source": [
25+
"<a href=\"https://colab.research.google.com/github/mr7495/image-classification-spatial/blob/main/Sub_ImageNet_ResNet50_Depthw_constraints_224.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"metadata": {
31+
"id": "WMJOLaJwx7Tg"
32+
},
33+
"source": [
34+
"#This code shows the implementation of our proposed depthwise convolution layer method with constraints for enhancing image classification by extracting spatial data\n",
35+
"#Images resolution is 224*224"
36+
],
37+
"execution_count": null,
38+
"outputs": []
39+
},
40+
{
41+
"cell_type": "code",
42+
"metadata": {
43+
"id": "t1SzEKNRzFL5",
44+
"colab": {
45+
"base_uri": "https://localhost:8080/"
46+
},
47+
"outputId": "e116e023-682b-487d-b1e2-71fbdb0a1305"
48+
},
49+
"source": [
50+
"!nvidia-smi #show GPU type"
51+
],
52+
"execution_count": null,
53+
"outputs": [
54+
{
55+
"output_type": "stream",
56+
"text": [
57+
"Thu Mar 18 11:05:16 2021 \n",
58+
"+-----------------------------------------------------------------------------+\n",
59+
"| NVIDIA-SMI 460.56 Driver Version: 460.32.03 CUDA Version: 11.2 |\n",
60+
"|-------------------------------+----------------------+----------------------+\n",
61+
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
62+
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
63+
"| | | MIG M. |\n",
64+
"|===============================+======================+======================|\n",
65+
"| 0 Tesla P100-PCIE... Off | 00000000:00:04.0 Off | 0 |\n",
66+
"| N/A 44C P0 27W / 250W | 0MiB / 16280MiB | 0% Default |\n",
67+
"| | | N/A |\n",
68+
"+-------------------------------+----------------------+----------------------+\n",
69+
" \n",
70+
"+-----------------------------------------------------------------------------+\n",
71+
"| Processes: |\n",
72+
"| GPU GI CI PID Type Process name GPU Memory |\n",
73+
"| ID ID Usage |\n",
74+
"|=============================================================================|\n",
75+
"| No running processes found |\n",
76+
"+-----------------------------------------------------------------------------+\n"
77+
],
78+
"name": "stdout"
79+
}
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"metadata": {
85+
"id": "6mw_i4iuzHlC"
86+
},
87+
"source": [
88+
"import numpy as np\n",
89+
"from tensorflow import keras\n",
90+
"from tensorflow.keras import layers\n",
91+
"import cv2\n",
92+
"import zipfile\n",
93+
"import shutil\n",
94+
"import random\n",
95+
"import pandas as pd\n",
96+
"import csv\n",
97+
"import os"
98+
],
99+
"execution_count": 1,
100+
"outputs": []
101+
},
102+
{
103+
"cell_type": "code",
104+
"metadata": {
105+
"id": "uP8qFi9uWzAG",
106+
"colab": {
107+
"base_uri": "https://localhost:8080/"
108+
},
109+
"outputId": "2f58bd9c-14ad-48c6-dce0-e3f3102814b2"
110+
},
111+
"source": [
112+
"#download Sub-ImageNet dataset from Kaggle (https://www.kaggle.com/mohammadrahimzadeh/imagenet-70classes)\n",
113+
"#Get a new download like from kaggle website at the mentioned URL, then replace the link with the \"kaggle_linke\" input in the next line of code \n",
114+
"\n",
115+
"!wget -cO - kaggle_link > imagenet_70classes.zip #replace kaggle_link with a new download link from https://www.kaggle.com/mohammadrahimzadeh/imagenet-70classes"
116+
],
117+
"execution_count": 2,
118+
"outputs": [
119+
{
120+
"output_type": "stream",
121+
"text": [
122+
"--2021-04-22 04:07:43-- https://storage.googleapis.com/kaggle-data-sets/941422/1595734/bundle/archive.zip?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=gcp-kaggle-com%40kaggle-161607.iam.gserviceaccount.com%2F20210422%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20210422T040732Z&X-Goog-Expires=259199&X-Goog-SignedHeaders=host&X-Goog-Signature=41a809988838ab3e7d5e1be0de968e66b4adeb247df4a80ae4db4aaf34b1750a0d427e19f050c2f37cd2b8484091c490b5383e1fcc766f312e3d2692f63f73a12593d0acd601c0a0675d1a405ab227ef23ad76d30869502d7c3e5ee8ac396c2ec9177c1906669e44648b424b79f84bd1ba8ec68527a52af2e11954a31049b5752b60ec635716cab575ed85616139579c9a68bc098256539b48231d3a66e6f6775a8e013384516c029d0e4332bc7dbc4ea41709f3c66628fe8076c72a09bf760d93d26faddf07c3d0bea0739cc8f249e0b09b3f67206324e7fcb93cd65e3054fd20effc3c2e46c99cea94eaa438ddbe6571d296c99ffa52404f29dc2b8e190154\n",
123+
"Resolving storage.googleapis.com (storage.googleapis.com)... 172.217.15.112, 172.217.164.144, 142.250.73.208, ...\n",
124+
"Connecting to storage.googleapis.com (storage.googleapis.com)|172.217.15.112|:443... connected.\n",
125+
"HTTP request sent, awaiting response... 200 OK\n",
126+
"Length: 2753347742 (2.6G) [application/zip]\n",
127+
"Saving to: ‘STDOUT’\n",
128+
"\n",
129+
"- 100%[===================>] 2.56G 45.2MB/s in 36s \n",
130+
"\n",
131+
"2021-04-22 04:08:19 (72.8 MB/s) - written to stdout [2753347742/2753347742]\n",
132+
"\n"
133+
],
134+
"name": "stdout"
135+
}
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"metadata": {
141+
"id": "pWNFXlWBWzDg"
142+
},
143+
"source": [
144+
"archive = zipfile.ZipFile('imagenet_70classes.zip') #Extract Sub-ImageNet Dataset\n",
145+
"for file in archive.namelist():\n",
146+
" archive.extract(file, 'data')"
147+
],
148+
"execution_count": 3,
149+
"outputs": []
150+
},
151+
{
152+
"cell_type": "code",
153+
"metadata": {
154+
"id": "79KB2ItAzHqb"
155+
},
156+
"source": [
157+
"#Set data augmentation techniques\n",
158+
"train_datagen = keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True,vertical_flip=True\n",
159+
" ,zoom_range=0.2,rotation_range=360\n",
160+
" ,width_shift_range=0.1,height_shift_range=0.1\n",
161+
" ,channel_shift_range=50\n",
162+
" ,brightness_range=(0,1.2)\n",
163+
" ,preprocessing_function=keras.applications.imagenet_utils.preprocess_input)\n",
164+
"\n",
165+
"test_datagen = keras.preprocessing.image.ImageDataGenerator(preprocessing_function=keras.applications.imagenet_utils.preprocess_input)\n",
166+
"\n",
167+
"train_df = pd.read_csv(\"data/train.csv\")\n",
168+
"test_df = pd.read_csv(\"data/test.csv\")"
169+
],
170+
"execution_count": 4,
171+
"outputs": []
172+
},
173+
{
174+
"cell_type": "code",
175+
"metadata": {
176+
"id": "sq8125qva0PJ"
177+
},
178+
"source": [
179+
"#Replace '\\\\' with '/' in CSV files\n",
180+
"for i in range(len(train_df['filename'])):\n",
181+
" name=train_df['filename'][i]\n",
182+
" index=name.index('\\\\')\n",
183+
" new_name=name[:index]+'/'+name[index+1:]\n",
184+
" train_df['filename'][i]=new_name\n",
185+
"\n",
186+
"for i in range(len(test_df['filename'])):\n",
187+
" name=test_df['filename'][i]\n",
188+
" index=name.index('\\\\')\n",
189+
" new_name=name[:index]+'/'+name[index+1:]\n",
190+
" test_df['filename'][i]=new_name"
191+
],
192+
"execution_count": 5,
193+
"outputs": []
194+
},
195+
{
196+
"cell_type": "code",
197+
"metadata": {
198+
"id": "L3dqLamH0TBw",
199+
"colab": {
200+
"base_uri": "https://localhost:8080/"
201+
},
202+
"outputId": "b50fde4d-4223-4f29-cca6-364d992b3ae7"
203+
},
204+
"source": [
205+
"#Create Data augmentation techniques\n",
206+
"\n",
207+
"batch_size=70\n",
208+
"train_generator = train_datagen.flow_from_dataframe(\n",
209+
" dataframe=train_df,\n",
210+
" directory='data',\n",
211+
" x_col=\"filename\",\n",
212+
" y_col=\"class\",\n",
213+
" target_size=(224, 224),\n",
214+
" batch_size=batch_size,\n",
215+
" class_mode='categorical',shuffle=True)\n",
216+
"validation_generator = test_datagen.flow_from_dataframe(\n",
217+
" dataframe=test_df,\n",
218+
" directory='data',\n",
219+
" x_col=\"filename\",\n",
220+
" y_col=\"class\",\n",
221+
" target_size=(224, 224),\n",
222+
" batch_size=batch_size,\n",
223+
" class_mode='categorical',shuffle=True)"
224+
],
225+
"execution_count": 6,
226+
"outputs": [
227+
{
228+
"output_type": "stream",
229+
"text": [
230+
"Found 31500 validated image filenames belonging to 70 classes.\n",
231+
"Found 3500 validated image filenames belonging to 70 classes.\n"
232+
],
233+
"name": "stdout"
234+
}
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"metadata": {
240+
"id": "yo9nEsSx7OfK"
241+
},
242+
"source": [
243+
"name=\"Sub-ImageNet-ResNet50-Depthw-constraints-224\"\n",
244+
"!mkdir \"models\" #create new folder for saving checkpoints\n",
245+
"!mkdir \"reports\" #create new folder for saving evaluation reports\n",
246+
"keras.backend.clear_session() #clear backend\n",
247+
"shape=(224,224,3) \n",
248+
"input_tensor=keras.Input(shape=shape)\n",
249+
"base_model=keras.applications.ResNet50(input_tensor=input_tensor,weights=None,include_top=False)\n",
250+
"depth=keras.layers.DepthwiseConv2D(tuple(base_model.output.shape[1:3]), depthwise_initializer=keras.initializers.RandomNormal(mean=0.0,stddev=0.01),\n",
251+
" bias_initializer=keras.initializers.Zeros(),depthwise_constraint=keras.constraints.NonNeg())(base_model.output)\n",
252+
"flat=keras.layers.Flatten()(depth)\n",
253+
"preds=keras.layers.Dense(70,activation='softmax',\n",
254+
" kernel_initializer=keras.initializers.RandomNormal(mean=0.0,stddev=0.01),\n",
255+
" bias_initializer=keras.initializers.Zeros(),)(flat)\n",
256+
"model=keras.Model(inputs=base_model.input, outputs=preds) \n",
257+
"\n",
258+
"##################################\n",
259+
"for layer in model.layers:\n",
260+
" layer.trainable = True\n",
261+
"model.summary()\n",
262+
"filepath=\"models/%s-{epoch:02d}-{val_accuracy:.4f}.hdf5\"%name\n",
263+
"checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', save_best_only=False, mode='max') #creating checkpoint to save the best validation accuracy\n",
264+
"callbacks_list = [checkpoint]\n",
265+
"\n",
266+
"#Determine adaptive learning rate with an initialization value of 0.045 and decay of 0.94 every two epochs. 31500 is the number of training images\n",
267+
"lr_schedule =keras.optimizers.schedules.ExponentialDecay(\n",
268+
" initial_learning_rate=0.045,\n",
269+
" decay_steps=2*int(31500/batch_size),\n",
270+
" decay_rate=0.94,\n",
271+
" staircase=True)\n",
272+
"optimizer=keras.optimizers.SGD(momentum=0.9,learning_rate=lr_schedule)\n",
273+
"model.compile(optimizer=optimizer, loss='categorical_crossentropy',metrics=['accuracy'])\n",
274+
"hist=model.fit_generator(train_generator, epochs=148,validation_data=validation_generator,shuffle=True,callbacks=callbacks_list) #start training\n",
275+
"with open('reports/{}.csv'.format(name), mode='w',newline='') as csv_file: #write reports\n",
276+
" csv_writer = csv.writer(csv_file, delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n",
277+
" for key in hist.history:\n",
278+
" data=[key]\n",
279+
" data.extend(hist.history[key])\n",
280+
" csv_writer.writerow(data)\n",
281+
"print(\"Training finished. Reports saved!\")"
282+
],
283+
"execution_count": null,
284+
"outputs": []
285+
}
286+
]
287+
}

0 commit comments

Comments
 (0)