Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepare .npy data using Numpy for input for CNN

I am new to python. I have .npy file for input for my CNN model. So many examples out there is using keras and I'm not allowed to use that. So, I want to read 1 array on my .npy file. For example, my file consist of pixels of images :

[ [ 120, 120],
  [ 120, 120],
  .................,
  [ 120, 120] ] 

There are 20 lines. If i use input = np.load(myfile.npy) then input.shape() the result is of course (20, 2). I can't use that for my model. Because the input should be (120,120).

So how can I read 1 array in that file? Or maybe you can tell me the best way to use own image for CNN. Thank you, Sorry for bad English :)

https://drive.google.com/open?id=1wmI3wO2ePDmZW5loFf2DsgDD9Og0lhyU the image file and it's label

like image 862
Amy Dafe Avatar asked Nov 28 '18 09:11

Amy Dafe


People also ask

How do I save a NumPy array as a csv file?

Use the numpy. savetxt() Function to Save a NumPy Array in a CSV File. The savetxt() function from the numpy module can save an array to a text file. We can specify the file format, delimiter character, and many other arguments to get the final result in our desired format.

How do I write a NumPy array to a file in Python?

Let us see how to save a numpy array to a text file. Creating a text file using the in-built open() function and then converting the array into string and writing it into the text file using the write() function. Finally closing the file using close() function.


1 Answers

Your Problem

It seems that you have saved the data in the wrong way. After your last comment, I found the initial problem.

Currently, you use this data.append(pixel_value.shape) and then you save this as .npy. What you are actually doing here, is saving the dimensions of the data and not the data itself.

So, when I load the .npy file from the link that you posted, I have this:

array([[  1, 120, 120],
       [  1, 120, 120],
       [  1, 120, 120],
       [  1, 120, 120],

You are saving the dimensions of the pixel_value.


How to solve this

So, use this to date the actual data: data.append(pixel_value).

Then I should be trivial how to load the file:

data_all = np.load('file.npy')

# get the first image
img1 = data_all[0]
like image 132
seralouk Avatar answered Nov 15 '22 06:11

seralouk