Skip to content

Commit eba8a19

Browse files
committed
Initial commit
0 parents  commit eba8a19

File tree

61 files changed

+94788
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+94788
-0
lines changed

--global

Whitespace-only changes.

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.csv
2+
*.jpg
3+
*.h5
4+
*.npz
5+
*.hdf5
6+
*.gz
7+
*.xml
8+
*.DS_Store
9+
10+
11+
12+
ModelCheckpoints/
13+
Preprocessing/
14+
Submission/
15+
DL3 Dataset/
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 77,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"import numpy as np\n",
12+
"from keras.models import Sequential\n",
13+
"from keras.layers import Dense\n",
14+
"from keras.layers import Dropout\n",
15+
"from keras.layers import Flatten\n",
16+
"from keras.layers.convolutional import Convolution2D\n",
17+
"from keras.layers.convolutional import MaxPooling2D\n",
18+
"from keras.layers import merge, Input\n",
19+
"from keras.utils import np_utils\n",
20+
"from keras.models import Model, Sequential\n",
21+
"from keras import backend as K\n",
22+
"K.set_image_dim_ordering('th')\n",
23+
"\n",
24+
"# fix random seed for reproducibility\n",
25+
"seed = 7\n",
26+
"np.random.seed(seed)"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 78,
32+
"metadata": {
33+
"collapsed": false
34+
},
35+
"outputs": [],
36+
"source": [
37+
"import pandas as pd\n",
38+
"root_dir=\"/home/delhivery\"\n",
39+
"rel_path=\"/Desktop/dataset\"\n",
40+
"train=pd.read_csv(root_dir+rel_path+\"/Train/train.csv\")\n",
41+
"test=pd.read_csv(root_dir+rel_path+\"/Test.csv\")"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 79,
47+
"metadata": {
48+
"collapsed": false
49+
},
50+
"outputs": [
51+
{
52+
"data": {
53+
"text/plain": [
54+
"(60000,)"
55+
]
56+
},
57+
"execution_count": 79,
58+
"metadata": {},
59+
"output_type": "execute_result"
60+
}
61+
],
62+
"source": [
63+
"from keras.datasets import mnist\n",
64+
"(X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
65+
"y_train.shape"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 80,
71+
"metadata": {
72+
"collapsed": false
73+
},
74+
"outputs": [],
75+
"source": [
76+
"import scipy.misc\n",
77+
"import cv2\n",
78+
"train_size=30000\n",
79+
"test_size=19000\n",
80+
"X_train=np.zeros((train_size,28,28))\n",
81+
"y_train=np.zeros((train_size,1))\n",
82+
"X_test=np.zeros((test_size,28,28))\n",
83+
"y_test=np.zeros((test_size,1))"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 83,
89+
"metadata": {
90+
"collapsed": false
91+
},
92+
"outputs": [],
93+
"source": [
94+
"\n",
95+
"for image_index in range(train_size):\n",
96+
" img=scipy.misc.imread(root_dir+rel_path+\"/Train/Images/train/\"+train.filename[image_index])\n",
97+
" img = cv2.cvtColor( img, cv2.COLOR_RGB2GRAY )\n",
98+
" X_train[image_index]=img\n",
99+
" y_train[image_index]=train.label[image_index]\n",
100+
"\n",
101+
"for image_index in range(test_size):\n",
102+
" img=scipy.misc.imread(root_dir+rel_path+\"/Train/Images/train/\"+train.filename[train_size+image_index])\n",
103+
" img=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)\n",
104+
" X_test[image_index]=img\n",
105+
" y_test[image_index]=train.label[train_size+image_index] "
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 113,
111+
"metadata": {
112+
"collapsed": false
113+
},
114+
"outputs": [],
115+
"source": [
116+
"test.shape\n",
117+
"from keras.models import load_model\n",
118+
"from keras.models import model_from_json\n",
119+
"\n",
120+
"model= load_model('my_model.h5')"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 114,
126+
"metadata": {
127+
"collapsed": false
128+
},
129+
"outputs": [],
130+
"source": [
131+
"img_test=np.zeros((21000,28,28))\n",
132+
"for i in range(len(test)):\n",
133+
" img=scipy.misc.imread(root_dir+rel_path+\"/Train/Images/test/\"+test.filename[i])\n",
134+
" img = cv2.cvtColor( img, cv2.COLOR_RGB2GRAY )\n",
135+
" img_test[i]=img\n",
136+
" "
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 115,
142+
"metadata": {
143+
"collapsed": false
144+
},
145+
"outputs": [],
146+
"source": [
147+
"img_test = img_test.reshape(img_test.shape[0], 1, 28, 28).astype('float32')\n",
148+
"img_test=img_test / 255\n",
149+
"\n",
150+
"\n"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 118,
156+
"metadata": {
157+
"collapsed": true
158+
},
159+
"outputs": [],
160+
"source": [
161+
"json_string = model.to_json()\n",
162+
"model.save_weights('my_model_weights.h5')"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 119,
168+
"metadata": {
169+
"collapsed": false
170+
},
171+
"outputs": [
172+
{
173+
"name": "stdout",
174+
"output_type": "stream",
175+
"text": [
176+
"21000/21000 [==============================] - 180s \n"
177+
]
178+
}
179+
],
180+
"source": [
181+
"preds = model.predict (img_test, verbose=1)\n"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": null,
187+
"metadata": {
188+
"collapsed": true
189+
},
190+
"outputs": [],
191+
"source": []
192+
}
193+
],
194+
"metadata": {
195+
"anaconda-cloud": {},
196+
"kernelspec": {
197+
"display_name": "Python [conda root]",
198+
"language": "python",
199+
"name": "conda-root-py"
200+
},
201+
"language_info": {
202+
"codemirror_mode": {
203+
"name": "ipython",
204+
"version": 3
205+
},
206+
"file_extension": ".py",
207+
"mimetype": "text/x-python",
208+
"name": "python",
209+
"nbconvert_exporter": "python",
210+
"pygments_lexer": "ipython3",
211+
"version": "3.5.2"
212+
}
213+
},
214+
"nbformat": 4,
215+
"nbformat_minor": 1
216+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from keras.models import load_model
2+
import scipy.misc
3+
import cv2
4+
import pandas as pd
5+
import operator
6+
import numpy as np
7+
root_dir="/home/delhivery"
8+
rel_path="/Desktop/dataset"
9+
test=pd.read_csv(root_dir+rel_path+"/Test.csv")
10+
model= load_model('my_model3.h5')
11+
img_test=np.zeros((21000,28,28))
12+
for i in range(len(test)):
13+
img=scipy.misc.imread(root_dir+rel_path+"/Train/Images/test/"+test.filename[i])
14+
img = cv2.cvtColor( img, cv2.COLOR_RGB2GRAY )
15+
img_test[i]=img
16+
img_test = img_test.reshape(img_test.shape[0], 1, 28, 28).astype('float32')
17+
img_test=img_test / 255
18+
preds = model.predict (img_test, verbose=1)
19+
index=[]
20+
for ind in range(len(test)):
21+
index.append(max(enumerate(preds[ind]), key=operator.itemgetter(1))[0])
22+
23+
test['label']=index
24+
test.to_csv('mythirdSubmission.csv',index=False)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import numpy
2+
from keras.datasets import mnist
3+
from keras.models import Sequential
4+
from keras.layers import Dense
5+
from keras.layers import Dropout
6+
from keras.layers import Flatten,Activation
7+
from keras.layers.convolutional import Convolution2D
8+
from keras.layers.convolutional import MaxPooling2D
9+
from keras.layers import Input
10+
from keras.utils import np_utils
11+
from keras import backend as K
12+
from keras.layers.normalization import BatchNormalization
13+
K.set_image_dim_ordering('th')
14+
from keras.callbacks import ModelCheckpoint
15+
weightsOutputFile = 'Digit_classifier.{epoch:02d}-{val_loss:.3f}.hdf5'
16+
# fix random seed for reproducibility
17+
seed = 7
18+
numpy.random.seed(seed)
19+
20+
# load data
21+
(X_train, y_train), (X_test, y_test) = mnist.load_data()
22+
print(X_train.shape)
23+
print(X_test.shape)
24+
# reshape to be [samples][pixels][width][height]
25+
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
26+
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
27+
28+
# normalize inputs from 0-255 to 0-1
29+
X_train = X_train / 255
30+
X_test = X_test / 255
31+
# one hot encode outputs
32+
y_train = np_utils.to_categorical(y_train)
33+
y_test = np_utils.to_categorical(y_test)
34+
num_classes = y_test.shape[1]
35+
36+
def CNN_Model():
37+
model = Sequential()
38+
39+
model.add(Convolution2D(7, 5, 5,border_mode='valid', input_shape=(1, 28, 28),name='conv1_1'))
40+
model.add(BatchNormalization())
41+
model.add(Activation('relu'))
42+
model.add(Convolution2D(7, 5, 5,border_mode='valid', name='conv1_2'))
43+
model.add(BatchNormalization())
44+
model.add(Activation('relu'))
45+
model.add(MaxPooling2D((2, 2), strides=(1, 1)))
46+
47+
model.add(Convolution2D(14, 3, 3, border_mode='valid', name='conv2_1'))
48+
model.add(BatchNormalization())
49+
model.add(Activation('relu'))
50+
model.add(Convolution2D(14, 3, 3,border_mode='valid', name='conv2_2'))
51+
model.add(BatchNormalization())
52+
model.add(Activation('relu'))
53+
model.add(MaxPooling2D((2, 2), strides=(1, 1)))
54+
55+
model.add(Convolution2D(28, 1, 1,border_mode='valid', name='conv3_1'))
56+
model.add(BatchNormalization())
57+
model.add(Activation('relu'))
58+
model.add(Convolution2D(28, 1, 1,border_mode='valid', name='conv3_2'))
59+
model.add(BatchNormalization())
60+
model.add(Activation('relu'))
61+
model.add(MaxPooling2D((2, 2), strides=(1, 1)))
62+
63+
64+
model.add(Dropout(0.2))
65+
model.add(Flatten())
66+
model.add(Dense(112))
67+
model.add(BatchNormalization())
68+
model.add(Activation('relu'))
69+
model.add(Dense(56))
70+
model.add(BatchNormalization())
71+
model.add(Activation('relu'))
72+
model.add(Dense(10))
73+
model.add(BatchNormalization())
74+
model.add(Activation('softmax'))
75+
# Compile model
76+
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
77+
return model
78+
79+
80+
model=CNN_Model()
81+
checkpointer = ModelCheckpoint(weightsOutputFile, monitor='accuracy', save_best_only=False, mode='auto')
82+
model.fit(X_train, y_train, batch_size=100, nb_epoch=20,verbose=1, validation_data=(X_test, y_test),callbacks=[checkpointer])
83+
scores = model.evaluate(X_test, y_test, verbose=1)
84+
print("Baseline Error: %.2f%%" % (100 - scores[1] * 100))

DeepLearning-Playground/DeepLearning-Specialization/Module-1/.idea/Module-1.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)