The article does not write clearly please forgive Qaq
In this article we will make a very simple image classifier with the CIFAR-10 data set. The CIFAR-10 dataset contains 60,000 images. In this dataset, there are 10 different categories, with 6,000 images in each category. The size of each image is x 32 pixels. While such a small size often poses difficulties in identifying the right category for humans, it is actually a simplification of the computer model and reduces the computational complexity required to analyze the image.
CIFAR-10 Data Set
We can input these images into our model by entering a number sequence of the models. Each pixel is identified by three floating-point numbers, which represent the red, green, and blue values (RGB values) of the pixel. So each image has a value of x 3 x = 3,072 values of 0.
With very large convolutional neural networks you can achieve high-quality results, and you can learn Rodrigo Benenson's page in this connection
Download CIFAR-10 DataSet, URL: Python version of the dataset, and install him under the folder where our classifier code resides
First on the source code
Source code for the model:
ImportNumPy as NPImportTensorFlow as TFImport TimeImportData_helpersbegintime=time.time () batch_size= 100learning_rate= 0.005max_steps= 1000data_sets=Data_helpers.load_data ()#Define input PlaceholdersImages_placeholder = Tf.placeholder (Tf.float32, Shape=[none, 3072]) Labels_placeholder= Tf.placeholder (Tf.int64, shape=[None])#Define variables (These is the values we want to optimize)weights = tf. Variable (Tf.zeros ([3072, 10])) biases= TF. Variable (Tf.zeros ([10]))#Define The classifier ' s resultLogits = Tf.matmul (Images_placeholder, weights) +biases#Define the loss functionLoss = Tf.reduce_mean (Tf.nn.sparse_softmax_cross_entropy_with_logits (logits=logits, Labels=labels_placeholder))#Define the training operationTrain_step =Tf.train.GradientDescentOptimizer (learning_rate). Minimize (loss)#operation comparing prediction with true labelCorrect_prediction = Tf.equal (Tf.argmax (logits, 1), Labels_placeholder)#operation calculating the accuracy of our predictionsaccuracy =Tf.reduce_mean (Tf.cast (correct_prediction, Tf.float32)) with TF. Session () as Sess:#Initialize VariablesSess.run (Tf.global_variables_initializer ())#Repeat max_steps Times forIinchRange (max_steps):#Generate input Data batchindices = Np.random.choice (data_sets['Images_train'].shape[0], batch_size) Images_batch= data_sets['Images_train'][indices] Labels_batch= data_sets['Labels_train'][indices]#periodically print out of the model ' s current accuracy ifI% 100 = =0:train_accuracy= Sess.run (accuracy, feed_dict={images_placeholder:images_batch, labels_placeholder:labels_batch})Print('Step {: 5d}: Training accuracy {: g}'. Format (i, train_accuracy))#Perform A single training stepSess.run (Train_step, feed_dict={images_placeholder:images_batch, labels_placeholder:labels_batch})#After finishing the training, evaluate on the test setTest_accuracy = Sess.run (accuracy, feed_dict={images_placeholder:data_sets['images_test'], labels_placeholder:data_sets['labels_test']}) Print('Test accuracy {: g}'. Format (test_accuracy)) EndTime=time.time ()Print('Total time: {: 5.2f}s'. Format (Endtime-begintime))
Code for working with datasets
ImportNumPy as NPImportPickleImportSYSdefload_cifar10_batch (filename):" "load data from single CIFAR-10 file" "with open (filename,'RB') as F:ifSys.version_info[0] < 3: Dict=pickle.load (f)Else: Dict= Pickle.load (f, encoding='latin1') x= dict['Data'] y= dict['Labels'] x=X.astype (float) y=Np.array (y)returnx, ydefload_data ():" "load all CIFAR-10 data and merge training batches" "XS=[] Ys= [] forIinchRange (1, 6): filename='Cifar-10-batches-py/data_batch_'+Str (i) X, Y=load_cifar10_batch (filename) xs.append (X) ys.append (Y) X_train=np.concatenate (XS) y_train=np.concatenate (YS)delxs, Ys X_test, Y_test= Load_cifar10_batch ('Cifar-10-batches-py/test_batch') Classes= ['plane','Car','Bird','Cat','Deer','Dog','Frog','Horse', ' Ship','Truck'] #Normalize DataMean_image = Np.mean (X_train, axis=0) X_train-=mean_image x_test-=mean_image data_dict= { 'Images_train': X_train,'Labels_train': Y_train,'images_test': X_test,'labels_test': Y_test,'Classes': Classes}returndata_dictdefReshape_data (data_dict): Im_tr= Np.array (data_dict['Images_train']) Im_tr= Np.reshape (Im_tr, (-1, 3, 32, 32)) Im_tr= Np.transpose (Im_tr, (0, 2, 3, 1)) data_dict['Images_train'] =im_tr Im_te= Np.array (data_dict['images_test']) Im_te= Np.reshape (Im_te, (-1, 3, 32, 32)) Im_te= Np.transpose (Im_te, (0, 2, 3, 1)) data_dict['images_test'] =Im_tereturndata_dictdefgen_batch (data, Batch_size, Num_iter): Data=Np.array (data) Index=len (data) forIinchRange (num_iter): Index+=batch_sizeif(Index + batch_size >len (data)): Index=0 shuffled_indices=np.random.permutation (len (data)) data (Np.arange )=Data[shuffled_indices]yieldData[index:index +Batch_size]defMain (): Data_sets=Load_data ()Print(data_sets['Images_train'].shape)Print(data_sets['Labels_train'].shape)Print(data_sets['images_test'].shape)Print(data_sets['labels_test'].shape)if __name__=='__main__': Main ()
First we imported the TensorFlow numpy time and the Data_help package we wrote.
Time is to calculate how long the entire code is running. DATA_HELP is the data structure that we use to train the dataset.
Load_data () in data_help will divide 60000 Cifar datasets into two pieces: 500000 training sets and 100000 test sets, specifically returning a dictionary with the following content
images_train
: Training set. A 500000-sheet containing 3072 (32x32 pixel x3 color channel) value
labels_train
: 50,000 tags of the training set (0 to 9 per label, which represents the 10 categories to which the training image belongs)
images_test
: Test Set (3,072)
labels_test
: 10,000 tags in test set
classes
: 10 text tags for converting numeric class values to words (e.g. 0 for ' plane ', 1 for ' car ')
Realization of a simple image classifier using TensorFlow neural network