-
Notifications
You must be signed in to change notification settings - Fork 5
/
data.py
49 lines (37 loc) · 1.66 KB
/
data.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
# Necessary Imports
import cv2
import pathlib
import numpy as np
import pandas as pd
import tensorflow as tf
import os
# View an image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import random
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
device = 'cuda' if tf.test.is_gpu_available() else 'cpu'
# Path to Kaggle Input
path = "C:/Users/Tumul Ranjan/OneDrive/Documents/monument-prediction/Indian-monuments/images"
# Walk through the directory and list number of files
for dirpath, dirnames, filenames in os.walk(path):
print(f"There are {len(dirnames)} directories and {len(filenames)} images in '{dirpath}'.")
# append the training and the testing paths to the original path
train_dir = path + "/train/"
test_dir = path + "/test/"
# get all the class names
data_dir = pathlib.Path(train_dir)
class_names = np.array(sorted([item.name for item in data_dir.glob("*")]))
# Rescale the data and create data generator instances
train_datagen = ImageDataGenerator(rescale=1/255,)
test_datagen = ImageDataGenerator(rescale=1/255,)
# Load data in from directories and turn it into batches
train_data = train_datagen.flow_from_directory(train_dir,
target_size=(300, 300),
batch_size=16,
class_mode='categorical')
test_data = test_datagen.flow_from_directory(test_dir,
target_size=(300, 300),
batch_size=16,
class_mode='categorical')