Skip to content
Snippets Groups Projects
Commit 738a729d authored by Tanja Vähämäki's avatar Tanja Vähämäki
Browse files

Vain koodi näkyvillä

parent 87151539
No related branches found
No related tags found
No related merge requests found
Pipeline #10006 passed
Source diff could not be displayed: it is too large. Options to address this: view the blob.
Source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:code id: tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
```
%% Cell type:code id: tags:
``` python
DATADIR="C:/Users/Omistaja/Documents/kagglecatsanddogs_3367a/PetImages2"
```
%% Cell type:code id: tags:
``` python
CATEGORIES=["Dog","Cat"]
# CATEGORIES=["Dog","Cat","Butterfly"]
```
%% Cell type:code id: tags:
``` python
for category in CATEGORIES:
path=os.path.join(DATADIR,category)
for img in os.listdir(path):
img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array,cmap="gray")
plt.show()
break
break
```
%% Cell type:code id: tags:
``` python
print(img_array.shape)
```
%% Cell type:code id: tags:
``` python
IMG_SIZE=50
```
%% Cell type:code id: tags:
``` python
new_array=cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
```
%% Cell type:code id: tags:
``` python
plt.imshow(new_array,cmap='gray')
```
%% Cell type:code id: tags:
``` python
plt.show()
```
%% Cell type:code id: tags:
``` python
training_data=[]
```
%% Cell type:code id: tags:
``` python
def create_training_data():
for category in CATEGORIES:
path=os.path.join(DATADIR,category)
class_num=CATEGORIES.index(category)
for img in os.listdir(path):
try:
img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
new_array=cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
training_data.append([new_array,class_num])
except Exception as e:
pass
create_training_data()
```
%% Cell type:code id: tags:
``` python
print(len(training_data))
```
%% Cell type:code id: tags:
``` python
import random
random.shuffle(training_data)
```
%% Cell type:code id: tags:
``` python
for sample in training_data[:10]:
print(sample[1])
plt.imshow(sample[0], cmap='gray')
plt.show()
```
%% Cell type:code id: tags:
``` python
X=[]
y=[]
```
%% Cell type:code id: tags:
``` python
for features, label in training_data:
X.append(features)
y.append(label)
X=np.array(X).reshape(-1,IMG_SIZE,IMG_SIZE,1)
```
%% Cell type:code id: tags:
``` python
import pickle
pickle_out=open("X.pickle","wb")
pickle.dump(X,pickle_out)
pickle_out.close()
pickle_out=open("y.pickle","wb")
pickle.dump(y,pickle_out)
pickle_out.close()
```
%% Cell type:code id: tags:
``` python
pickle_in=open("X.pickle","rb")
X=pickle.load(pickle_in)
pickle_in=open("y.pickle","rb")
y=pickle.load(pickle_in)
```
%% Cell type:code id: tags:
``` python
X[1]
```
%% Cell type:code id: tags:
``` python
y[1]
```
%% Cell type:code id: tags:
``` python
import tensorflow as tf
```
%% Cell type:code id: tags:
``` python
from tensorflow.keras.models import Sequential
```
%% Cell type:code id: tags:
``` python
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
```
%% Cell type:code id: tags:
``` python
from tensorflow.keras.callbacks import TensorBoard # lisätty
import time # lisätty
```
%% Cell type:code id: tags:
``` python
from tensorflow.keras.datasets import cifar10 # lisätty
```
%% Cell type:code id: tags:
``` python
from tensorflow.keras.preprocessing.image import ImageDataGenerator # lisätty
```
%% Cell type:code id: tags:
``` python
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333) # lisätty
sess=tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) # lisätty
file_writer=tf.summary.FileWriter('C:/Users/Omistaja/Anaconda3/Scripts/logs/',sess.graph) # lisätty
```
%% Cell type:code id: tags:
``` python
X = X/255.0
```
%% Cell type:code id: tags:
``` python
dense_layers=[0]
```
%% Cell type:code id: tags:
``` python
layer_sizes=[64]
layer_sizes=[32]
```
%% Cell type:code id: tags:
``` python
conv_layers=[3]
conv_layers=[4]
```
%% Cell type:code id: tags:
``` python
for dense_layer in dense_layers:
for layer_size in layer_sizes:
for conv_layer in conv_layers:
NAME = "{}-conv-{}-nodes-{}-dense-{}".format(conv_layer,layer_size,dense_layer,int(time.time()))
print(NAME)
tensorboard=TensorBoard(log_dir='C:/Users/Omistaja/Anaconda3/Scripts/logs/{}'.format(NAME)) # lisätty
```
%% Cell type:code id: tags:
``` python
model=Sequential()
model.add(Conv2D(layer_size,(3,3),input_shape=X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
```
%% Cell type:code id: tags:
``` python
for l in range(conv_layer-1):
model.add(Conv2D(layer_size,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
```
%% Cell type:code id: tags:
``` python
model.add(Flatten())
for l in range(dense_layer):
model.add(Dense(layer_size))
model.add(Activation('relu'))
```
%% Cell type:code id: tags:
``` python
model.add(Dense(1))
model.add(Activation('sigmoid'))
```
%% Cell type:code id: tags:
``` python
model.compile(loss="binary_crossentropy",optimizer="adam",metrics=['accuracy'])
```
%% Cell type:code id: tags:
``` python
model.fit(X,y,batch_size=32, epochs=10, validation_split=0.1, callbacks=[tensorboard])
```
%% Cell type:code id: tags:
``` python
model.save('CatsOrDogsSmall64x3-CNN.model')
model.save('CatsOrDogsSmall32x4-CNN.model')
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment