how to resize cifar10 image from 32x32 to 227x227?

6,641 views
Skip to first unread message

xingxi...@gmail.com

unread,
Nov 9, 2016, 6:14:50 AM11/9/16
to Keras-users
I have read the image from cifar-10-batches-python
import os
import numpy as np
from PIL import Image
from pylab import *
import matplotlib.pyplot as plt
from scipy.misc import imresize
# read data
data_dir = "F:\\dataSet\\cifar-10-batches-py"
testdata_dir="F:\\dataSet\\cifar-10-batches-py\\test_batch"
da=np.load(testdata_dir)
testdata=da['data']
testlabel=np.array(da['labels'])

train=np.empty((50000,3072))
label=np.empty((50000,))
for i in range(1,2):
    str='data_batch_'+np.str(i)
    path1=os.path.join(data_dir,str)
    data=np.load(path1)
    train[10000*(i-1):10000*i,:]=data['data']
    label[10000*(i-1):10000*i,]=data['labels']

def intlabel(label):
    for i in range(label.shape[0]):
        label[i,]=int(float(label[i,]))
    return label

def intdata(data):
    n=data.shape[0]
    for i in range(n):
        for j in range(3072):
            data[i,j]=int(float(data[i,j]))
    return data

label,train=intlabel(label),intdata(train)
train,label=np.array(train),np.array(label)


then I don't know how to resize the data.

Klemen Grm

unread,
Nov 10, 2016, 10:34:07 AM11/10/16
to Keras-users
from keras.datasets import cifar10
import cv2
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

data_upscaled = np.zeros((50000, 3, 227, 227))
for i, img in enumerate(X_train):
    im = img.reshape((1, 2, 0))
    large_img = cv2.resize(im, dsize=(227, 227), interpolation=cv2.INTER_CUBIC)
    data_upscaled[i] = large_img.reshape((2, 0, 1))

gopida...@gmail.com

unread,
Nov 13, 2017, 11:21:21 AM11/13/17
to Keras-users
Hey I tried this in keras and I'm getting error like,

cannot reshape array of size 3072 into shape (1,2,0)


Message has been deleted

f.ortega...@gmail.com

unread,
Sep 14, 2018, 2:43:56 PM9/14/18
to Keras-users
Try this one (from Klemen Grm answer):

import numpy as np
from keras.datasets import cifar10
import cv2
(X_train, y_train), (X_test, y_test) = cifar10.load_data()


data_upscaled
= np.zeros(50000, 3, 227, 227)

for i, img in enumerate(X_train):

    im
= np.transpose(img, (1, 2, 0))

    large_img
= cv2.resize(im, dsize=(227, 227), interpolation=cv2.INTER_CUBIC)

    data_upscaled
[i] = np.transpose(large_img, (2, 0, 1))

Regards

khaoula boutiche

unread,
Mar 25, 2022, 7:24:44 AM3/25/22
to Keras-users

hey!
please can you explain to me why we transpose !!

Lance Norskog

unread,
Mar 25, 2022, 5:44:54 PM3/25/22
to khaoula boutiche, Keras-users
The input data is stored as 3 single-color images, R, G, and B, or "channels-first" format. We store it as one image with 3 colors per pixel or "channels-last format".
Transpose() rearranges the indexes to achieve this. Here is a better explanation:


--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/7c3672bf-b7b7-49a1-9f41-c631da8b0171n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA

François Chollet

unread,
Mar 26, 2022, 1:56:39 PM3/26/22
to Lance Norskog, khaoula boutiche, Keras-users
Reply all
Reply to author
Forward
0 new messages