I'll ask you a question for the first time. When I predicted an image using Xception in keras.applications, the value changed according to batch_size. How can I get the prediction results regardless of batch_size? Is some setting required? I'm sorry, but I'd appreciate it if you could teach me somebody. Thank you.
Thanks to tiitoi, the output layer has the same result.
However, since we are using anomaly detection in Metric Learning, the value we want is the value of the Xception middle layer.
Sorry for not enough words.
How can I get the value of the middle layer of Xception regardless of batch_size?
import numpy as np from keras.models import Model from keras.applications import Xception image_data = Image data image_data = np.asarray (image_data) image_data = image_data.astype ('float32')/255.0 for i in range (100): # Build Xception model base_model = Xception (include_top = True, weights = 'imagenet', input_shape = (299, 299, 3)) model = Model (inputs = base_model.input, outputs = base_model.layers [i + 2] .output) p1 = model.predict (image_data, batch_size = 1) p32 = model.predict (image_data, batch_size = 32) judge = (p1 == p32) .all () print (str (i) + ':' + str (judge)) model.summary () print () if not judge: break
When I tried the above code, it seemed that there was a difference in block1_conv2.Supplemental information (FW/tool version etc.)
The environment is as follows.
Ubuntu 18.04.3 LTS
Answer # 1
Since floating-point operations cause rounding errors, it is not normal to compareAdditional
==to see if they are exactly the same.
If it is numpy, it can be compared with numpy.allclose, so it is better to judge here.
import numpy as np from PIL import Image from tensorflow.keras.applications import Xception from tensorflow.keras.models import Model # Build Xception model model = Xception (include_top = True, weights = "imagenet", input_shape = (299, 299, 3)) img = Image.open ("sample.jpg"). resize ((299, 299)) img = np.float32 (img)/255 image_data = np.array ([img for i in range (50)]) base_model = Xception (include_top = True, weights = "imagenet", input_shape = (299, 299, 3)) for i in range (100): model = Model (inputs = base_model.input, outputs = base_model.layers [i + 2] .output) p1 = model.predict (image_data, batch_size = 1) p2 = model.predict (image_data, batch_size = 32) if not np.allclose (p1, p2, rtol = 1e-03): print (p1.ravel () [: 10]) print (p2.ravel () [: 10]) break
- python - [pytorch] i want to get the output of the middle layer of a complicated model
- python - absolute value behavior of complex numbers in numpy
- put the maximum value of the list of variables in the objective function with python pulp
- i want to get the value from a constant in python and display it
- i want to retrieve the elements in the middle of the difference sequence as a list python
- python 3x - how to get the value of scrolledtext
- python - how to output the key and value in the dictionary in any form
- i want to set the maximum value of the slider in python to the number entered in the text box
- i want to dynamically change the value of a variable in python and execute it
- (python) i would like to know how to combine an int value and a multidimensional array
- i want to retrieve a cell value from python
- python - how to enable selection by value when selecting a selection format column
- substitute the value of a specific line of csv read into python
- python - i want to make a total value of channels with multiple images
- python - [ajax] i want to get and send the value of the button element in the form
- python - exception value erorr catch location
- python - i want to get only the value of the first stage (after roipool) of faster r-cnn of torchvision
- python - i want to get the very first value of the corresponding condition in the time condition of pandas
- if the value of the variable to which the variable is assigned is changed in python, the value will change to the original varia
- python - list comprehension? how to retrieve the value of
- python : How do I bring sorted data from Neuraleta?
- python : About the size of y_pred, y_true in Keras
- python : Working with a small amount of data for binary classification in neural networks
- python : RESOURCEEXHAUSTEDERRORRORR ERROR During neural network training
- python : Valueerror error: Invalid Literal for int () with Base 10 When running Estimator.Train string (input_fn= train_input_fn
- python : 95% Accuracy Predicts Wrong Class (Multiclass Classification Kaggle GTSRB -German Traffic Sign Recognition Benchmark)
- python : TensorFlow Cudart64_101.dll Not Found
- python : How to make the neural set classify the text?
- Using Weights in FIT () with Python Keras-RL
- python : Permissionerror: [errno 13] permission denied