def inference(self, image_list): """Do an inference on the model with a set of inputs. # Arguments: image_list: The input image list Return the result of the inference. """ image = image_list[0] image = self.linear_to_srgb(image) image = (image * 255).astype(np.uint8) kernel = self.kernel_size * 2 + 1 blur = cv2.GaussianBlur(image, (kernel, kernel), 0) blur = blur.astype(np.float32) / 255. blur = self.srgb_to_linear(blur) # If make_blur button is pressed in Nuke if self.make_blur: script_msg = message_pb2.FieldValuePairAttrib() script_msg.name = "PythonScript" # Create a Python script message to run in Nuke python_script = self.blur_script(blur) script_msg_val = script_msg.values.add() script_msg_str = script_msg_val.string_attributes.add() script_msg_str.values.extend([python_script]) return [blur, script_msg] return [blur]
def inference(self, image_list): """Do an inference on the model with a set of inputs. # Arguments: image_list: The input image list Return the result of the inference. """ image = image_list[0] image = self.linear_to_srgb(image).copy() image = (image * 255).astype(np.uint8) if not hasattr(self, 'model'): # Initialise tensorflow graph tf.compat.v1.reset_default_graph() config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth=True self.sess=tf.compat.v1.Session(config=config) # Load most recent trained model self.model = self.load_model() self.class_labels = (self.checkpoint_name.split('.')[0]).split('_') # If checkpoint name has changed, load new checkpoint if self.prev_ckpt_name != self.checkpoint_name or self.checkpoint_name == '': self.model = self.load_model() self.class_labels = (self.checkpoint_name.split('.')[0]).split('_') # If checkpoint correctly loaded, update previous checkpoint name self.prev_ckpt_name = self.checkpoint_name image = cv2.resize(image, dsize=(224, 224), interpolation=cv2.INTER_NEAREST) # Predict on new data image_batch = np.expand_dims(image, 0) # Preprocess a numpy array encoding a batch of images (RGB values within [0, 255]) image_batch = tf.keras.applications.mobilenet.preprocess_input(image_batch) start = time.time() y_prob = self.model.predict(image_batch) y_class = y_prob.argmax(axis=-1)[0] duration = time.time() - start # Print results on server side print('Inference duration: {:4.3f}s'.format(duration)) class_scores = str(["{0:0.4f}".format(i) for i in y_prob[0]]).replace("'", "") print("Class scores: {} --> Label: {}".format(class_scores, self.class_labels[y_class])) # If get_label button is pressed in Nuke if self.get_label: # Send back which class was detected script_msg = message_pb2.FieldValuePairAttrib() script_msg.name = "PythonScript" # Create a Python script message to run in Nuke nuke_msg = "Class scores: {}\\nLabel: {}".format(class_scores, self.class_labels[y_class]) python_script = "nuke.message('{}')\n".format(nuke_msg) script_msg_val = script_msg.values.add() script_msg_str = script_msg_val.string_attributes.add() script_msg_str.values.extend([python_script]) return [image_list[0], script_msg] return [image_list[0]]
def inference(self, image_list): """Do an inference on the model with a set of inputs. # Arguments: image_list: The input image list Return the result of the inference. """ image = image_list[0] image = linear_to_srgb(image).copy() if not hasattr(self, 'sess'): # Initialise tensorflow graph tf.compat.v1.reset_default_graph() config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.compat.v1.Session(config=config) # Input is stacked histograms of original and gamma-graded images. input_shape = [1, 2, 100] # Initialise input placeholder size self.input = tf.compat.v1.placeholder(tf.float32, shape=input_shape) self.model = baseline_model( input_shape=input_shape[1:], output_param_number=self.output_param_number) self.infer_op = self.model(self.input) # Load latest model checkpoint self.saver = tf.compat.v1.train.Saver() self.load(self.model) self.prev_ckpt_name = self.checkpoint_name # If checkpoint name has changed, load new checkpoint if self.prev_ckpt_name != self.checkpoint_name or self.checkpoint_name == '': self.load(self.model) # If checkpoint correctly loaded, update previous checkpoint name self.prev_ckpt_name = self.checkpoint_name # Preprocess image same way we preprocessed it for training # Here for gamma correction compute histograms def histogram(x, value_range=[0.0, 1.0], nbins=100): """Return histogram of tensor x""" h, w, c = x.shape hist = tf.histogram_fixed_width(x, value_range, nbins=nbins) hist = tf.divide(hist, h * w * c) return hist with tf.compat.v1.Session() as sess: # Convert to grayscale img_gray = tf.image.rgb_to_grayscale(image) img_gray = tf.image.resize(img_gray, [self.patch_size, self.patch_size]) # Apply gamma correction img_gray_grade = tf.math.pow(img_gray, self.gamma_to_predict) img_grade = tf.math.pow(image, self.gamma_to_predict) # Compute histograms img_hist = histogram(img_gray) img_grade_hist = histogram(img_gray_grade) hists_op = tf.stack([img_hist, img_grade_hist], axis=0) hists, img_grade = sess.run([hists_op, img_grade]) res_img = srgb_to_linear(img_grade) hists_batch = np.expand_dims(hists, 0) start = time.time() # Run model inference inference = self.sess.run(self.infer_op, feed_dict={self.input: hists_batch}) duration = time.time() - start print('Inference duration: {:4.3f}s'.format(duration)) res = inference[-1] print("Predicted gamma: {}".format(res)) # If predict button is pressed in Nuke if self.predict: script_msg = message_pb2.FieldValuePairAttrib() script_msg.name = "PythonScript" # Create a Python script message to run in Nuke python_script = self.nuke_script(res) script_msg_val = script_msg.values.add() script_msg_str = script_msg_val.string_attributes.add() script_msg_str.values.extend([python_script]) return [res_img, script_msg] return [res_img]