In [15]:
import cv2, os
import numpy as np
In [25]:
xs=[]
ys=[]
for f in os.listdir("dog"):
if f.endswith(".jpg"):
file_path=os.path.join("dog", f)
print(f)
img=cv2.imread(file_path,0)
h,w=img.shape
if w<h:
margin=(h-w)//2
croped=img[ margin:-margin, :]
else:
margin=(w-h)//2
croped=img[ :, margin:-margin]
resized=cv2.resize(croped,(28,28))
x=np.reshape(resized,784).astype(float)/255.0
xs.append(x)
ys.append([0,1])
batch=[np.array(xs),np.array(ys,dtype=float)]
2fa81c13-e1f9-497c-a145-a131ad432f82.jpg
3e58641e-e40e-4baf-98eb-db723b443910.jpg
3efce652-6388-4845-acf4-4f0efc3dbc5e.jpg
795c7ebd-c596-4b8d-886a-d5091567b7e6.jpg
7f3b7443-363e-44de-9e85-8b3659132b78.jpg
87813e8f-4dce-4871-a1b9-b998ccfb6de5.jpg
9a9d2742-5566-4714-b08e-b84de8daada6.jpg
b4bf89a4-1a53-4f6e-9a51-b18ff15ef953.jpg
c43610e1-d6b8-4655-b60c-1d8f8fa4fd2f.jpg
In [31]:
import pickle
with open('dataset.pickle', 'wb') as f:
pickle.dump(batch, f, protocol=pickle.HIGHEST_PROTOCOL)
In [34]:
with open('dataset.pickle', 'rb') as f:
batch_load=pickle.load(f)
In [39]:
np.allclose(batch[0], batch_load[0])
Out[39]:
True
In [40]:
np.allclose(batch[1], batch_load[1])
Out[40]:
True
In [36]:
batch_load
Out[36]:
[array([[ 0.91764706, 0.88627451, 0.85098039, ..., 0.2 ,
0.13333333, 0.18431373],
[ 0.62745098, 0.55686275, 0.67843137, ..., 0.44313725,
0.39607843, 0.46666667],
[ 0.48235294, 0.48627451, 0.4745098 , ..., 0.83137255,
0.81568627, 0.81176471],
...,
[ 0.49803922, 0.42745098, 0.46666667, ..., 0.41176471,
0.38039216, 0.41960784],
[ 1. , 1. , 1. , ..., 1. ,
1. , 1. ],
[ 0.16862745, 0.17254902, 0.16078431, ..., 0.32156863,
0.14901961, 0.02745098]]), array([[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.]])]
In [33]:
ls -l
total 664
-rw-r--r-- 1 admin staff 3846 Nov 5 14:23 Loader.ipynb
-rw-r--r-- 1 admin staff 268873 Nov 5 14:44 Resize.ipynb
-rw-r--r--@ 1 admin staff 114 Nov 5 13:15 __init__.py
drwxr-xr-x 2 admin staff 68 Nov 5 13:08 cat/
-rw-r--r-- 1 admin staff 800 Nov 5 14:41 data.h5
-rw-r--r-- 1 admin staff 56783 Nov 5 14:44 dataset.pickle
drwxr-xr-x 12 admin staff 408 Nov 5 14:23 dog/
In [26]:
batch[0].shape
Out[26]:
(9, 784)
In [27]:
batch[1].shape
Out[27]:
(9, 2)
In [6]:
%matplotlib inline
from matplotlib import pyplot as plt
plt.imshow(img, 'gray')
Out[6]:
<matplotlib.image.AxesImage at 0x11665b240>
In [8]:
plt.imshow(croped,'gray')
Out[8]:
<matplotlib.image.AxesImage at 0x116de2780>
In [11]:
resized=cv2.resize(croped,(28,28))
In [13]:
plt.imshow(resized,'gray')
Out[13]:
<matplotlib.image.AxesImage at 0x116cab1d0>
In [18]:
x=np.reshape(resized,784)
In [20]:
x.astype(float)/255.0
Out[20]:
array([ 0.16862745, 0.17254902, 0.16078431, 0.18431373, 0.21176471,
0.18431373, 0.16470588, 0.0745098 , 0.15294118, 0.16078431,
0.15294118, 0.15686275, 0.15294118, 0.13333333, 0.16078431,
0.16862745, 0.14117647, 0.16470588, 0.16470588, 0.15686275,
0.15294118, 0.17254902, 0.15294118, 0.14901961, 0.14117647,
0.15294118, 0.16470588, 0.14117647, 0.14901961, 0.14901961,
0.16078431, 0.18039216, 0.16470588, 0.16470588, 0.12941176,
0.1372549 , 0.14117647, 0.14509804, 0.16470588, 0.16470588,
0.14117647, 0.10980392, 0.04705882, 0.16078431, 0.09411765,
0.16078431, 0.16470588, 0.14901961, 0.12941176, 0.1254902 ,
0.1254902 , 0.14901961, 0.18431373, 0.17254902, 0.16470588,
0.15294118, 0.14509804, 0.16078431, 0.17647059, 0.14901961,
0.12941176, 0.21960784, 0.31372549, 0.1372549 , 0.1254902 ,
0.1372549 , 0.16078431, 0.60392157, 0.6745098 , 0.73333333,
0.62745098, 0.74117647, 0.64313725, 0.7372549 , 0.18039216,
0.1372549 , 0.14509804, 0.12156863, 0.1372549 , 0.16862745,
0.14509804, 0.14901961, 0.15686275, 0.16078431, 0.10980392,
0.23921569, 0.25098039, 0.21176471, 0.27058824, 0.23921569,
0.20784314, 0.16862745, 0.25098039, 0.18823529, 0.65490196,
0.91372549, 0.50196078, 0.97254902, 0.8627451 , 0.96078431,
0.9254902 , 0.83137255, 0.6627451 , 0.22745098, 0.10588235,
0.09019608, 0.19607843, 0.17647059, 0.17254902, 0.17254902,
0.15294118, 0.16078431, 0.18039216, 0.20784314, 0.14509804,
0.23137255, 0.28627451, 0.15294118, 0.21568627, 0.25882353,
0.22745098, 0.65490196, 0.70980392, 0.97254902, 0.99607843,
0.74509804, 0.96862745, 0.97647059, 0.98431373, 0.94509804,
0.6745098 , 0.58431373, 0.15294118, 0.21568627, 0.18039216,
0.16078431, 0.16470588, 0.16862745, 0.16078431, 0.14901961,
0.19215686, 0.15686275, 0.09803922, 0.14509804, 0.15294118,
0.14509804, 0.23137255, 0.21568627, 0.23529412, 0.56078431,
0.72156863, 0.54117647, 0.59215686, 0.54509804, 0.69803922,
0.70588235, 0.96862745, 0.7254902 , 0.74901961, 0.67058824,
0.61176471, 0.22352941, 0.19607843, 0.18431373, 0.2 ,
0.15294118, 0.16862745, 0.14901961, 0.09411765, 0.09019608,
0.07843137, 0.21568627, 0.2745098 , 0.20784314, 0.24705882,
0.14901961, 0.21176471, 0.94509804, 0.94509804, 0.56078431,
0.63921569, 0.61960784, 0.50980392, 0.7372549 , 0.75686275,
0.69411765, 0.6627451 , 0.69411765, 0.61176471, 0.16470588,
0.22352941, 0.16078431, 0.17254902, 0.18039216, 0.13333333,
0.14509804, 0.05882353, 0.10196078, 0.25490196, 0.18039216,
0.30588235, 0.21960784, 0.21960784, 0.1254902 , 0.65490196,
0.94901961, 0.96470588, 0.65098039, 0.6 , 0.61960784,
0.6627451 , 0.90980392, 0.95686275, 0.96470588, 0.89019608,
0.29411765, 0.19607843, 0.21960784, 0.30588235, 0.18823529,
0.18039216, 0.1372549 , 0.15294118, 0.14117647, 0.10980392,
0.21568627, 0.18823529, 0.15686275, 0.16862745, 0.16470588,
0.1254902 , 0.64705882, 0.1372549 , 0.7254902 , 0.03921569,
0.69411765, 0.5254902 , 0.84313725, 0.78431373, 0.70196078,
0.45490196, 0.81568627, 0.25490196, 0.24313725, 0.28627451,
0.36862745, 0.17254902, 0.25098039, 0.20392157, 0.18431373,
0.1372549 , 0.02745098, 0.16470588, 0.18431373, 0.05490196,
0.06666667, 0.05098039, 0.05098039, 0.43529412, 0.71372549,
0.66666667, 0.26666667, 0.11372549, 0.58823529, 0.14901961,
0.18431373, 0.32941176, 0.79607843, 0.73333333, 0.72941176,
0.74901961, 0.72941176, 0.21176471, 0.2745098 , 0.16078431,
0.24313725, 0.2745098 , 0.34117647, 0.26666667, 0.11372549,
0.21960784, 0.10588235, 0.27058824, 0.1254902 , 0.05882353,
0.04705882, 0.53333333, 0.64705882, 0.10980392, 0.05490196,
0.46666667, 0.13333333, 0.1254902 , 0.14901961, 0.09019608,
0.29019608, 0.74117647, 0.69803922, 0.18431373, 0.71764706,
0.38039216, 0.32156863, 0.25098039, 0. , 0.01568627,
0.19607843, 0.26666667, 0.14117647, 0.23921569, 0.18431373,
0.32156863, 0.29803922, 0.09411765, 0.04313725, 0.43529412,
0.17647059, 0.4 , 0.23921569, 0.16862745, 0.09411765,
0.05490196, 0.0627451 , 0.05882353, 0.07843137, 0.51764706,
0.78431373, 0.74509804, 0.76470588, 0.20784314, 0.21568627,
0.27058824, 0.2 , 0.00784314, 0.07058824, 0.12941176,
0.11764706, 0.13333333, 0.05490196, 0.04313725, 0.05098039,
0.04705882, 0.04705882, 0.10588235, 0.20392157, 0.50980392,
0.09803922, 0.16470588, 0.14117647, 0.09019608, 0.08235294,
0.0745098 , 0.15294118, 0.23921569, 0.23921569, 0.62745098,
0.09019608, 0.68235294, 0.24313725, 0.23137255, 0.29411765,
0.16470588, 0.01568627, 0.05490196, 0.27058824, 0.08627451,
0.07058824, 0.04313725, 0.04313725, 0.03921569, 0.05882353,
0.05098039, 0.4 , 0.82352941, 0.89803922, 0.30588235,
0.57647059, 0.08235294, 0.46666667, 0.39215686, 0.17254902,
0.28627451, 0.17254902, 0.31764706, 0.72156863, 0.70196078,
0.35294118, 0.16470588, 0.30588235, 0.26666667, 0.00784314,
0.04705882, 0.11372549, 0.15686275, 0.03921569, 0.0627451 ,
0.09803922, 0.10980392, 0.10588235, 0.18039216, 0.03921569,
0.29019608, 0.37254902, 0.23137255, 0.32156863, 0.54117647,
0.31764706, 0.21176471, 0.44705882, 0.27843137, 0.85882353,
0.4 , 0.72156863, 0.72156863, 0.7254902 , 0.20784314,
0.32156863, 0.14901961, 0.0745098 , 0.01176471, 0.14901961,
0.39215686, 0.0745098 , 0.10588235, 0.10980392, 0.12941176,
0.1254902 , 0.08235294, 0.46666667, 0.06666667, 0.16862745,
0.40784314, 0.38039216, 0.19215686, 0.19607843, 0.38431373,
0.56862745, 0.67843137, 0.5372549 , 0.16470588, 0.71372549,
0.60392157, 0.74901961, 0.22352941, 0.2627451 , 0.21176471,
0.18039216, 0.13333333, 0.10980392, 0.4745098 , 0.11764706,
0.12156863, 0.11764706, 0.12941176, 0.11764706, 0.10196078,
0.0745098 , 0.30196078, 0.2745098 , 0.50588235, 0.54117647,
0.54117647, 0.36862745, 0.32941176, 0.28627451, 0.33333333,
0.08235294, 0.10196078, 0.72941176, 0.62352941, 0.59607843,
0.28235294, 0.32941176, 0.16862745, 0.17254902, 0.20784314,
0.18823529, 0.13333333, 0.52156863, 0.14901961, 0.15294118,
0.12156863, 0.35686275, 0.12941176, 0.16862745, 0.36470588,
0.38431373, 0.51372549, 0.69411765, 0.56078431, 0.62745098,
0.42745098, 0.3372549 , 0.14509804, 0.03137255, 0.73333333,
0.61568627, 0.68627451, 0.64313725, 0.40784314, 0.32156863,
0.15686275, 0.25490196, 0.22352941, 0.20784314, 0.11372549,
0.12941176, 0.50588235, 0.50980392, 0.15294118, 0.14901961,
0.12156863, 0.0627451 , 0.15294118, 0.29411765, 0.46666667,
0.43921569, 0.48627451, 0.43529412, 0.43529412, 0.37647059,
0.30196078, 0.12941176, 0.72941176, 0.65882353, 0.10588235,
0.68627451, 0.44705882, 0.0627451 , 0.20392157, 0.25882353,
0.21176471, 0.18039216, 0.13333333, 0.14509804, 0.18039216,
0.2 , 0.18823529, 0.19607843, 0.15686275, 0.03529412,
0.08235294, 0.22352941, 0.42352941, 0.37254902, 0.44705882,
0.38431373, 0.4 , 0.45098039, 0.44313725, 0.08235294,
0.26666667, 0.11764706, 0.65490196, 0.6745098 , 0.41960784,
0.01960784, 0.21960784, 0.21960784, 0.16862745, 0.14117647,
0.1254902 , 0.17254902, 0.18039216, 0.20392157, 0.20784314,
0.18431373, 0.30588235, 0.0745098 , 0.03529412, 0.01960784,
0.07843137, 0.01176471, 0.04313725, 0.11764706, 0.29803922,
0.11372549, 0.04313725, 0.02745098, 0.58823529, 0.71372549,
0.76862745, 0.63921569, 0.0745098 , 0.02745098, 0.21568627,
0.20784314, 0.20392157, 0.16862745, 0.21960784, 0.18823529,
0.22352941, 0.24313725, 0.24705882, 0.23137255, 0.23529412,
0.12156863, 0.03137255, 0.03529412, 0.03137255, 0.0627451 ,
0.04313725, 0.04705882, 0.05490196, 0.04313725, 0.06666667,
0.25490196, 0.70196078, 0.58823529, 0.51372549, 0.17647059,
0.10196078, 0.05882353, 0.01568627, 0.10980392, 0.14117647,
0.14509804, 0.21568627, 0.22745098, 0.20784314, 0.2745098 ,
0.25882353, 0.2627451 , 0.30980392, 0.24313725, 0.04705882,
0.03137255, 0.02745098, 0.02352941, 0.09411765, 0.36862745,
0.49411765, 0.42745098, 0.8627451 , 0.68235294, 0.01568627,
0.03137255, 0.01960784, 0.03529412, 0.21176471, 0.2 ,
0.07058824, 0.14509804, 0.32156863, 0.3254902 , 0.21960784,
0.22352941, 0.26666667, 0.27843137, 0.24705882, 0.25490196,
0.24705882, 0.32156863, 0.21568627, 0.05882353, 0.03529412,
0.09803922, 0.07058824, 0.02352941, 0.71764706, 0.58431373,
0.49803922, 0.01960784, 0.02352941, 0.02352941, 0.01960784,
0.03137255, 0.01568627, 0.22352941, 0.13333333, 0.06666667,
0.30196078, 0.32941176, 0.28235294, 0.30588235, 0.29019608,
0.27058824, 0.30196078, 0.29019608, 0.23137255, 0.19607843,
0.22745098, 0.21568627, 0.17647059, 0.14117647, 0.0745098 ,
0.0745098 , 0.0627451 , 0.03529412, 0.1372549 , 0.35686275,
0.32941176, 0.36078431, 0.32941176, 0.35294118, 0.3254902 ,
0.01568627, 0.24313725, 0.05882353, 0.22745098, 0.29803922,
0.23137255, 0.2745098 , 0.25098039, 0.30196078, 0.25098039,
0.31764706, 0.25882353, 0.28627451, 0.21568627, 0.20392157,
0.16862745, 0.11764706, 0.02745098, 0.38431373, 0.08627451,
0.27843137, 0.3372549 , 0.34117647, 0.30196078, 0.33333333,
0.31372549, 0.24705882, 0.30980392, 0.2745098 , 0.03921569,
0.06666667, 0.03137255, 0.31764706, 0.29411765, 0.23529412,
0.22745098, 0.18431373, 0.14117647, 0.24313725, 0.23529412,
0.27058824, 0.20784314, 0.19607843, 0.29411765, 0.08627451,
0.12941176, 0.36862745, 0.3372549 , 0.3254902 , 0.36078431,
0.35686275, 0.37647059, 0.36470588, 0.37254902, 0.36862745,
0.31372549, 0.32941176, 0.29019608, 0.01960784, 0.10196078,
0.02352941, 0.26666667, 0.28235294, 0.23921569, 0.30588235,
0.24705882, 0.28627451, 0.26666667, 0.31372549, 0.23529412,
0.37254902, 0.29411765, 0.03921569, 0.07843137, 0.36470588,
0.31764706, 0.28627451, 0.35294118, 0.29411765, 0.32941176,
0.40392157, 0.34509804, 0.37647059, 0.3372549 , 0.33333333,
0.27843137, 0.32156863, 0.14901961, 0.02745098])
In [ ]:
Content source: wasit7/cs634
Similar notebooks: