Fakermiya commited on
Commit
a6d0889
·
1 Parent(s): df2750b

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +160 -0
model.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import tensorflow as tf
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ import cv2
6
+ import os
7
+ import numpy as np
8
+
9
+ #data directories
10
+
11
+ DATADIR = "/home/spidy/Documents/RJIT/PicData"
12
+ CATEGORIES = ["sfw", "nsfw"]
13
+
14
+ for category in CATEGORIES:
15
+ path = os.path.join(DATADIR, category) #path to sfw and nfsw dir
16
+ for img in os.listdir(path):
17
+ img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
18
+ plt.imshow(img_array, cmap ="gray")
19
+ plt.show()
20
+ break
21
+ break
22
+
23
+
24
+ # In[2]:
25
+
26
+
27
+ print (img_array).shape
28
+
29
+
30
+ # In[3]:
31
+
32
+
33
+ IMG_SIZE = 80
34
+ new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
35
+ plt.imshow(new_array, cmap = 'gray')
36
+ plt.show()
37
+
38
+
39
+ # In[4]:
40
+
41
+
42
+ training_data = []
43
+ def create_training_data():
44
+ for category in CATEGORIES:
45
+ path = os.path.join(DATADIR, category) #path to sfw and nsfw
46
+ class_num = CATEGORIES.index(category)
47
+ for img in os.listdir(path):
48
+ try:
49
+ img_array = cv2.imread(os.path.join(path,img), cv2.IMREAD_GRAYSCALE)
50
+ new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
51
+ training_data.append([new_array, class_num])
52
+ except Exception as e:
53
+ pass
54
+
55
+
56
+ create_training_data()
57
+
58
+
59
+ # In[5]:
60
+
61
+
62
+ print(len(training_data))
63
+
64
+
65
+ # In[6]:
66
+
67
+
68
+ import random
69
+ random.shuffle(training_data)
70
+
71
+
72
+ # In[7]:
73
+
74
+
75
+ for sample in training_data[:10]:
76
+ print(sample[1])
77
+
78
+
79
+ # In[8]:
80
+
81
+
82
+ X = []
83
+ y = []
84
+
85
+
86
+
87
+ # In[9]:
88
+
89
+
90
+ for features, label in training_data:
91
+ X.append(features)
92
+ y.append(label)
93
+
94
+ X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
95
+
96
+
97
+ # In[10]:
98
+
99
+
100
+ import pickle
101
+
102
+ pickle_out = open("X.pickle", "wb")
103
+ pickle.dump(X, pickle_out)
104
+ pickle_out.close()
105
+
106
+ pickle_out = open("y.pickle", "wb")
107
+ pickle.dump(y, pickle_out)
108
+ pickle_out.close()
109
+
110
+
111
+ # In[11]:
112
+
113
+
114
+ pickle_in = open("X.pickle", "rb")
115
+ X = pickle.load(pickle_in)
116
+
117
+
118
+
119
+ # In[13]:
120
+
121
+
122
+ X[1]
123
+
124
+
125
+ # In[14]:
126
+
127
+
128
+ import tensorflow as tf
129
+ from tensorflow.keras.models import Sequential
130
+ from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
131
+ import pickle
132
+
133
+ X = pickle.load(open("X.pickle", "rb"))
134
+ y = pickle.load(open("y.pickle", "rb"))
135
+
136
+ X = X/255.0
137
+ y = np.array(y)
138
+
139
+ model = Sequential()
140
+ model.add(Conv2D(64, (3,3), input_shape = X.shape[1:]))
141
+ model.add(Activation("relu"))
142
+ model.add(MaxPooling2D(pool_size = (2, 2)))
143
+
144
+ model.add(Conv2D(64, (3,3)))
145
+ model.add(Activation("relu"))
146
+ model.add(MaxPooling2D(pool_size = (2, 2)))
147
+
148
+ model.add(Flatten())
149
+ model.add(Dense(64))
150
+
151
+ model.add(Dense(1))
152
+ model.add(Activation('sigmoid'))
153
+
154
+ model.compile(loss="binary_crossentropy",
155
+ optimizer="adam",
156
+ metrics=['accuracy'])
157
+
158
+ model.fit(X, y, batch_size=8, epochs=8, validation_split=0.1)
159
+
160
+