-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNIST_data_preparation.py
66 lines (52 loc) · 2.37 KB
/
MNIST_data_preparation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This is a first example. The goal is to solve the superresolution problem using MNIST dataset
# First of all we should divide the MNIST dataset into train images and test images
from tensorflow.keras.datasets import mnist
from PIL import Image
import os
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Find a way to only import the images?
# To re-arrange the code pulse Ctrl + Alt + L
# Create MNIST folder
base_dir = 'D:/MNIST' # naming the path
os.mkdir(base_dir) # creating the folder
# Create the folder for training
train_dir = 'D:/MNIST/training'
os.mkdir(train_dir)
# Create the folder for testing
test_dir = 'D:/MNIST/testing'
os.mkdir(test_dir)
# directory to store the dataset with smaller images
small_train = 'D:/MNIST/training/small_train'
os.mkdir(small_train)
# directory to store the dataset with normal images
normal_train = 'D:/MNIST/training/normal_train'
os.mkdir(normal_train)
# directory to store the dataset of testing smaller images
small_test = 'D:/MNIST/testing/small_test'
os.mkdir(small_test)
# directory to store the dataset of testing normal images
normal_test = 'D:/MNIST/testing/normal_test'
os.mkdir(normal_test)
# We create a loop for each TRAINING NORMAL image:
for i in range(len(train_images)):
array_image = train_images[i]
real_image = Image.fromarray(array_image)
# real_image.save(globals()['D:/MNIST/training/normal_train/normal_train_image%s.png' % i])
real_image.save('D:/MNIST/training/normal_train/normal_train_image' + str(i) + '.png')
# Same loop for testing
for i in range(len(test_images)):
array_image = test_images[i]
real_image = Image.fromarray(array_image)
real_image.save('D:/MNIST/testing/normal_test/normal_test_image' + str(i) + '.png')
# Now we can do the same but with the resize images
newImage_size = 14
for i in range(len(train_images)):
array_image = train_images[i]
real_image = Image.fromarray(array_image)
real_image = real_image.resize((newImage_size, newImage_size)) # Resizing the original image to a 14x14 one.
real_image.save('D:/MNIST/training/small_train/small_train_image' + str(i) + '.png')
for i in range(len(test_images)):
array_image = test_images[i]
real_image = Image.fromarray(array_image)
real_image = real_image.resize((newImage_size, newImage_size))
real_image.save('D:/MNIST/testing/small_test/small_test_image' + str(i) + '.png')