Note that the Inception_v3 training picture is of type (299,299,3), classified as 1001, so we need to convert the dataset to this format before making predictions, see read_files.py file; then we load inception_ V3 network and its given weights to predict, see test.py file, the training results are shown in the image below:
read_files.py
#coding =utf-8 import tensorflow as TF import numpy as NP import OS from PIL import Image def to_categorial (y,n_classes): Y_std=np.zeros ([Len (y), n_classes]) for I in range (len (y)): y_std[i,y[i]]=1.0 return y_std def getFile Arr (dir): result_arr=[] label_list=[] map={} map_file_result={} map_file_label={} map_new={} C
Ount_label=0 count=0 File_list=os.listdir (dir) for file in File_list:file_path=os.path.join (Dir,file) Label=file.split (".")
[0].split ("_") [0] Img=image.open (file_path) Result=np.array ([]) if (Len (Img.split ())!=3): map[file]= "hahaha" #os. Remove (file_path) Else:map[file]=label if label not In Label_list:label_list.append (label) Map_new[label]=count_label Count_ Label=count_label+1 r,g,b=img.split () R_arr=np.array (R). Reshape (299*299) g_arr=Np.array (g). Reshape (299*299) B_arr=np.array (b). Reshape (299*299) img_arr=np.concatenate ((R_arr,g_ar R,b_arr)) Result=np.concatenate ((Result,img_arr)) Result=result.reshape ((299,299,3)) m
Ap_file_result[file]=result result_arr.append (Result) count=count+1 for file in File_list:
If map[file]!= "hahaha": map_file_label[file]=map_new[map[file]] #map [File]=map_new[map[file]] Ret_arr=[] for file in file_list:if map[file]!= "hahaha": each_list=[] Label_one_ze Ro=np.zeros (Count_label) result=map_file_result[file] Label=map_file_label[file] #prin T (Label_one_zero) each_list.append (Result) each_list.append (label) Ret_arr.append (each _list) np.save (' Test02_299.npy ', Ret_arr) def load_data (Train_dir,test_dir): Train_data=np.load (Train_dir) tes T_data=np.load (Test_dir)
x_train_non,y_train_non=train_data[:,0],train_data[:,1] x_test_non,y_test_non=test_data[:,0],test_data[:,1] X_train=np.zeros ([Len (X_train_non), 299,299,3]) X_test=np.zeros ([Len (X_test_non), 299,299,3]) for I in Range (Len (x_t Rain_non)): X_train[i,:,:,:]=x_train_non[i] for I in range (len (X_test_non)): X_test[i,:,:,:]=x_test_no
N[i] #y_train_non =y_train_non.tolist () #y_test_non =y_test_non.tolist () y_train=to_categorial (y_train_non,1001) Y_test=to_categorial (y_test_non,1001) return (X_train,y_train), (x_test,y_test) def load_data (test_dir): t Est_data=np.load (Test_dir) x_test_non,y_test_non=test_data[:,0],test_data[:,1] X_test=np.zeros ([Len (X_test_non), 299,299,3]) for I in Range (len (X_test_non)): X_test[i,:,:,:]=x_test_non[i] Y_test=to_categorial (Y_test_non , 1001) return X_test,y_test
test.py
Import TensorFlow as TF import Tensorflow.contrib.slim as slim import tensorflow.contrib.slim.nets as Nets #from Tensorflo W.contrib.slim.nets.inception import Inception_v3, inception_v3_arg_scope from read_files import load_data import NumPy As NP height = 299 width = 299 channels = 3 test_dir= ' test02_299.npy ' # Create Graph X = Tf.placeholder (Tf.float32, shape =[none, height, width, channels]) with Slim.arg_scope (Nets.inception.inception_v3_arg_scope ()): logits, end_points = n Ets.inception.inception_v3 (X, num_classes=1001, is_training=false) predictions = end _points["predictions"] saver = tf.train.Saver () x_test,y_test = Load_data (test_dir) # Your images, shape [batch_size, 299 , 299, 3] # Execute graph with TF. Session () as Sess:saver.restore (Sess, "./inception_v3.ckpt") Predictions_val = Predictions.eval (feed_dict={x:x_t EST}) predicted_classes = Np.argmax (Predictions_val, Axis=1) print (predicted_classes)
The results are as follows:
We can see the corresponding prediction results, we can also match it with the original label to see the accuracy of the INCEPTION_V3 model.
PS1: A certain weight was not found in the Ckpt file
In training, the most frequent problem is: What weight is not found in the Ckpt file, but the corresponding weights have, it is very annoying, this time the problem may have several aspects:
1) The problem of the namespace, the weight file in a namespace, and load the corresponding weights from this weight file, the resulting weight and the expected weight with the namespace does not match, so can not find:
def model (x, Logits=false, Training=false): #print (' The model is called ', type (x), x.shape) #with Tf.variable_scope (' model ', reuse=true): With Slim.arg_scope (Nets.inception.inception_v1_arg_scope ()): Logits_, _ = Nets.incepti
ON.INCEPTION_V1 (x,num_classes=1001,is_training=training) y = Tf.nn.softmax (logits_) if logits: Return y,logits_ return y with Tf.variable_scope (' Model1 '): env.x = Tf.placeholder (Tf.float32, (None, Img_row S, Img_cols, Img_chas), name= ' x ') env.y = Tf.placeholder (Tf.float32, (None, n_classes), Nam E= ' y ') env.training = Tf.placeholder (bool, (), name= ' mode ') #print ' &&&&&&&&&& amp;& ' #print env.x.shape #print ' &&&&&&&&&&& ' Env.ybar, logit
s = Model (env.x, logits=true, training=env.training) with Slim.arg_scope (Nets.inception.inception_v1_arg_scope ()): INIT_FN = Slim.assIGN_FROM_CHECKPOINT_FN (' D://./inception_v1.ckpt ', Slim.get_model_variables ()) INIT_FN (Sess)
This time, even if the following slim.arg_scope with Tf.variable_scope (' Model1 ', reuse=true): Also useless, unable to match.
2) The given input data format does not match, for example, asked for input format (299,299,3), we gave (224,224,3), this time the error is still "in the Ckpt file found no weight", really very pit, looked for a long time to find the problem;
3) The corresponding weight of the file may really not have this weight, this time we are going to download a standard ckpt file, make sure to include the ownership value, the bottom to a can detect ckpt file in the name of the value of the code:
4) You may also encounter
Invalidargumenterror (see above for traceback): Assign requires shapes of both tensors to match. LHS shape= [+] RHS shape= [1001]
This error, do not be sad, the problem is:
Logits_,_=nets.inception.inception_v3 (x,reuse= None) here does not specify the num_classes=???, need to display specified to avoid this problem.
#coding =utf-8
import tensorflow as tf
import NumPy as NP from
Tensorflow.python import Pywrap_tensorflow
Reader=pywrap_tensorflow. Newcheckpointreader ("Inception_v3.ckpt")
var_to_shape_map=reader.get_variable_to_shape_map ()
print (var _TO_SHAPE_MAP) for
key in Var_to_shape_map:
#这儿可以输入你对应的报错的权值名字
if key== "inceptionv3/auxlogits/conv2d_ 2a_3x3/batchnorm/moving_variance ":
print (" found. ")
PS2: In addition to using saver to load weights, we can also load weights using the following methods:
INIT_FN = SLIM.ASSIGN_FROM_CHECKPOINT_FN (
os.path.join (checkpoints_dir, ' resnet_v1_101.ckpt '),
slim.get _model_variables (' resnet_v1_101 ')) with
TF. Session () as Sess:
init_fn (Sess)
For more details please see:
Github_ Migration Learning