I am writing a code that reads 50 pages each from a folder containing 100 images, converts them into a numpy array, and saves the npy file twice.
After the code was executed, two npy files were saved properly, but when I looked inside, it seemed that each of the npy files was saved 100 sheets.
I tried to randomly select 50 pages from within the folder in rn.sample, but I wonder if the writing method is wrong.
I'm sorry to bother you at such a busy time, but I'd appreciate it if you could give me some advice on how to fix it.
*I am also asking questions on teratail.As there was no response from you, I posted it here as well.I will share it with you as soon as I receive your reply.
import random as rn
img_size= (1000,500)
dir_name = './train'
file_type = 'jpeg'
img_list=glob.glob('./'+dir_name+'/*.'+file_type)
for i in range(2):
train_img_array_list = [ ]
Every time rn.sample(img_list,50)#i increases by 1, it reads 50 randomly selected images.
for img in img_list:
train_img=load_img(img,grayscale=True,target_size=(img_size))
train_img_array=img_to_array(train_img)/255
train_img_array_list.append(train_img_array)
train_img_array_list=np.array(train_img_array_list)
file_name = os.path.join('.', 'image' + '{0:04d}'.format(i) + '.npy') # Save npy file with serial number
np.save(file_name, train_img_array_list)
I received advice from teratail and corrected it as follows (img_list=rn.sample(img_list,50).I am very sorry for confusing you because I did not mention that the module had been imported as import random as rn when I asked.Thank you to everyone who took the time to review and make suggestions.
import random as rn
img_size= (1000,500)
dir_name = './train'
file_type = 'jpeg'
img_list=glob.glob('./'+dir_name+'/*.'+file_type)
for i in range(2):
train_img_array_list = [ ]
Img_list=rn.sample(img_list,50)#i reads 50 randomly selected images each time it increases by 1
for img in img_list:
train_img=load_img(img,grayscale=True,target_size=(img_size))
train_img_array=img_to_array(train_img)/255
train_img_array_list.append(train_img_array)
train_img_array_list=np.array(train_img_array_list)
file_name = os.path.join('.', 'image' + '{0:04d}'.format(i) + '.npy')
np.save(file_name, train_img_array_list)
I don't know how rn.sample works, so I don't think everyone can answer.
In addition, is the following helpful?
train_size=x_train.shape[0]
batch_size=10#10 randomly
batch_mask=np.random.choice(train_size, batch_size)
x_batch=x_train [batch_mask]
t_batch=t_train [batch_mask]
Source:
"Deep Learning from scratch -- Theory and Implementation of Deep Learning in Python"
(First Published July 28, 2017, O'Reilly Japan, Inc.)
© 2025 OneMinuteCode. All rights reserved.