-
Notifications
You must be signed in to change notification settings - Fork 5
/
Helper.py
122 lines (87 loc) · 3.64 KB
/
Helper.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
import json
import glob
import os
import Constants
import numpy as np
from PreProcessingModule import reshape_data
from keras.preprocessing.image import array_to_img, img_to_array, load_img
#Extracts the data from the small ship images along with their labels, divdes the data into training and testing data, and saves these into two seperate files
def extract_images():
os.chdir(Constants.SMALL_IMAGES_LOCATION)
full_data = []
full_labels = []
#Iterate through all images in the small image folder
for file in glob.glob("*.png"):
#Extract data from individual images
img = load_img(file)
img_array = img_to_array(img)
#Add data to arrays
full_data.append(img_array.astype(int))
full_labels.append(int(file[0]))
full_data = np.asarray(full_data)
#Shuffle data
concatenated_data = np.asarray(list(zip(full_data, full_labels)))
np.random.shuffle(concatenated_data)
#Divide dataset
test_dataset = concatenated_data[2500:]
training_dataset = concatenated_data[:2500]
test_data, test_labels = zip(*test_dataset)
training_data, training_labels = zip(*training_dataset)
#Create json objects to be exported to a file
json_test = {}
json_test["data"]=np.array(test_data)
json_test["labels"]=np.array(test_labels)
json_training = {}
json_training["data"]=np.array(training_data)
json_training["labels"]=np.array(training_labels)
#Save json objects into their respective files
test_file = open(Constants.OTHER_TEST_SMALL_IMAGE_DATASET, 'w')
training_file = open(Constants.OTHER_TRAINING_SMALL_IMAGE_DATASET, 'w')
np.set_printoptions(threshold=np.inf)
test_file.write(str(json_test))
training_file.write(str(json_training))
test_file.close()
training_file.close()
#Divides the original small ship dataset in the json file
def divide_data():
#Load full dataset from file
json_dataset = json.load(open(Constants.FULL_SMALL_IMAGE_DATASET))
#Extract necessary information from json
full_data = np.array(json_dataset['data'])
full_labels = np.array(json_dataset['labels'])
#Shuffle data
concatenated_data = np.asarray(list(zip(full_data, full_labels)))
np.random.shuffle(concatenated_data)
#Divide dataset
test_dataset = concatenated_data[2500:]
training_dataset = concatenated_data[:2500]
test_data, test_labels = zip(*test_dataset)
training_data, training_labels = zip(*training_dataset)
#Create json objects to be exported to a file
json_test = {}
json_test["data"]=np.array(test_data)
json_test["labels"]=np.array(test_labels)
json_training = {}
json_training["data"]=np.array(training_data)
json_training["labels"]=np.array(training_labels)
#Save json objects into their respective files
test_file = open(Constants.TEST_SMALL_IMAGE_DATASET, 'w')
training_file = open(Constants.TRAINING_SMALL_IMAGE_DATASET, 'w')
np.set_printoptions(threshold=np.inf)
test_file.write(str(json_test))
training_file.write(str(json_training))
test_file.close()
training_file.close()
#Returns the image data along with their respective labels
def load_data(data_location, data_size=(80,80)):
#Load data from file
json_dataset = json.load(open(data_location))
labels = json_dataset['labels']
data = []
#Turn data into a usuable format for a cnn
for img in json_dataset['data']:
data.append(np.transpose(np.resize(img, (3,80,80))))
data = reshape_data(data, data_size)
data = np.asarray(data)
return np.array(data), np.array(labels)