Home > other >  EMNIST data sets how to read
EMNIST data sets how to read

Time:04-09

Use the method of reading mnist dataset under tensorflow always read emnist dataset show that more than a few tags

CodePudding user response:

+ 1 is also don't know how to join in the neural network

CodePudding user response:

 
The import numpy as np
The import matplotlib. Pyplot as PLT
The import gzip

"" "
Just look the "parse_emnist API" and "load_data API
"
Parse_emnist: parse the emnist file with the extension - 'gz' to.. NPZ
Load_data: load the NPZ (the data structure is same as the 'mnist dataset of keras)

"" "


Def read_idx3 (filename) :
"" "
Read the given file with its name
: param filename: extension the name of the file is' gz '
: return: images data, shape - & gt; Num rows, cols
"" "
With gzip. Open (filename) as fo:
Print (' Reading images')
Buf=fo the read ()

Offset=0 # pointer
The header=np. Frombuffer (buf, '& gt; I ', 4, offset)
Magic_number, num_images num_rows, num_cols=header
Print (" the magic number: {} \ nnumber of images: {}, \ nnumber of rows: {}, \ nnumber of the columns: {} "\
. The format (magic_number, num_images num_rows, num_cols))

Offset +=header. The header size *. Itemsize
Data=https://bbs.csdn.net/topics/np.frombuffer (buf, '& gt; B ', num_rows num_images * * num_cols, offset). Reshape (
(num_images num_rows, num_cols))

The return data


Def read_idx1 (filename) :
"" "
Read the given file with its name
: param filename: extension the name of the file is' gz '
: return: labels
"" "
With gzip. Open (filename) as fo:
Print (' Reading labels')
Buf=fo the read ()

Offset=0
The header=np. Frombuffer (buf, '& gt; I ', 2, offset)
Magic_number, num_labels=header
Print (" the magic number: {}, \ nnumber of labels: {} "\
. The format (magic_number, num_labels))

Offset +=header. The header size *. Itemsize

Data=https://bbs.csdn.net/topics/np.frombuffer (buf, '& gt; B 'num_labels, offset)
The return data


Def show (images, labels, letter_mapping, window=(3, 4)) :
FIG, axes=PLT. Subplots (* window, figsize=(15, 15))
# FIG. Set_figheight (15)
# FIG. Set_figwidth (15)

For a row in the range (window [0]) :
For the column in the range (window [1]) :
Ind window [1]=* row + column
X=images (ind)
Y=labels (ind)

Axes/row (column). Imshow (x, cmap=PLT. Cm. Gray)
Axes/row (column). Set_title (letter_mapping [y], fontsize=30)
Axes [row] [r]. The column axis (' off ')
PLT. Tight_layout ()

PLT. The show ()


Def parse_emnist (output_path, * arg) :
"" "
: param output_path:
: param arg: four path - & gt; Train_img train_label, test_img, test_label
: return: None
"" "
Train_img_path train_label_path, test_img_path, test_label_path=arg
Train_img_data=https://bbs.csdn.net/topics/read_idx3 (train_img_path)
Train_label_data=https://bbs.csdn.net/topics/read_idx1 (train_label_path)
Test_img_data=https://bbs.csdn.net/topics/read_idx3 (test_img_path)
Test_label_data=https://bbs.csdn.net/topics/read_idx1 (test_label_path)

If np. Min (train_label_data)==1:
Train_label_data=1
https://bbs.csdn.net/topics/train_label_data -Test_label_data=1
https://bbs.csdn.net/topics/test_label_data -
Np. Savez (output_path,
X_train=train_img_data,
Y_train=train_label_data,
X_test=test_img_data,
Y_test=test_label_data)
Print (' Congratulations, your job has had done! Go to have a rest. ')


Def load_data (path) :
"" "Loads the EMNIST dataset.

# the Arguments
Path: the path where to load the dataset

# Returns
A Tuple of Numpy arrays: ` (x_train, y_train), (x_test, y_test) `.
"" "
F=np. The load (path)
X_train, y_train=f [' x_train], [' y_train] f
X_test, y_test=f [' x_test], [' y_test] f
F. lose ()
Return (x_train y_train), (x_test y_test)





CodePudding user response:

I am always more than labels, training data,
  • Related