text_plain_output_1.png
from joblib import Parallel, delayed

from torch.utils.data import Dataset, DataLoader

import numpy as np

import numpy as np

import numpy as np # linear algebra

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import torch

img_size = 32

def process_image(img_file):
    img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE)
    img = cv2.resize(img, (img_size, img_size))
    return img

start = time.time()
X_data = []
Y_data = []
for j in range(10):
    path = os.path.join('../input/state-farm-distracted-driver-detection/imgs/train', 'c' + str(j), '*.jpg')
    files = glob.glob(path)
    X_data.extend(Parallel(n_jobs=2)((delayed(process_image)(im_file) for im_file in files)))
    Y_data.extend([j] * len(files))
end = time.time() - start

X_data = np.array(X_data)
Y_data = np.array(Y_data)
np.random.shuffle(X_data)
np.random.shuffle(Y_data)
X_data = X_data[0:1000]
Y_data = Y_data[0:1000]

X_data = torch.Tensor(X_data)
X_data = X_data.flatten(start_dim=1)
X_data = X_data.numpy()
Y_data = np.reshape(Y_data, (-1, 1))

class Drivers_dataset(Dataset):
    def __init__(self, df):
        rows = df.shape[0]
        self.imgnp = df.iloc[:rows, 0:img_size * img_size].values
        self.labels = df.iloc[:rows, img_size * img_size].values
        self.rows = rows
    def __len__(self):
        return self.rows
    def __getitem__(self, idx):
        image = torch.tensor(self.imgnp[idx], dtype=torch.float) / 255
        image = image.view(1, img_size, img_size)
        label = self.labels[idx]
        return (image, label)

trainset = np.append(X_train, np.reshape(Y_train, (-1, 1)), axis=1)
testset = np.append(X_test, np.reshape(Y_test, (-1, 1)), axis=1)

testset = pd.DataFrame(data=testset)
trainset = pd.DataFrame(data=trainset)

trainset = Drivers_dataset(trainset)
testset = Drivers_dataset(testset)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=True, num_workers=2)
train_data_iter = iter(trainloader)
test_data_iter = iter(testloader)

dataiter = iter(trainloader)
images, labels = dataiter.next()
(images.size(), labels.size())