# Load model # encoder = Encoder() #encoder.train_step(random_sample.reshape((1, *random_sample.shape, 1)), parameters.reshape(1, *parameters.shape)) # encoder.load_weights(args.model) encoder_inputs, encoder_outputs = SimpleEncoderArchitecture((256, 256, 1)) encoder = Model(inputs=encoder_inputs, outputs=encoder_outputs) encoder.summary() encoder.load_weights(einfo['model_weights_path']) # Predict the parameters from the silhouette and generate a mesh prediction = encoder.predict(silhouette.reshape(1, 256, 256, 1)) prediction = tf.cast(prediction, tf.float64) print("Shape of predictions:'" + str(prediction.shape)) print(prediction[0, 82:85]) pred_pc, faces = smpl_model("../model.pkl", prediction[0, 72:82] * 0 + 1, prediction[0, :72], prediction[0, 82:85] * 0) # Render the mesh pred_mesh = Mesh(pointcloud=pred_pc.numpy()) pred_mesh.faces = faces if mesh is not None: mesh.render3D() print("Rendering prediction") pred_mesh.render3D() # Now render their silhouettes # cv2.imshow("True silhouette", silhouette) # pred_mesh.render_silhouette(title="Predicted silhouette")
def on_epoch_end(self, epoch, logs=None): """ Store the model loss and accuracy at the end of every epoch, and store a model prediction on data """ self.epoch_log.write( json.dumps({ 'epoch': epoch, 'loss': logs['loss'] }) + '\n') if (epoch + 1) % self.period == 0 or epoch == 0: # Predict on all of the given silhouettes for data_type, data in self.pred_data.items(): if data is not None: if not isinstance(data, list) or type(data) == np.array: data = np.array(data) data = data.reshape( (1, data.shape[0], data.shape[1], data.shape[2])) #for i, silhouette in enumerate(data): # # Save silhouettes # silhouette *= 255 # cv2.imwrite(os.path.join(self.pred_path, "{}_epoch.{:03d}.gt_silh_{:03d}.png".format(data_type, epoch + 1, i)), silhouette.astype("uint8")) preds = self.model.predict(data) #print("Predictions: " + str(preds)) for i, pred in enumerate(preds[1], 1): #self.smpl.set_params(pred[:72].reshape((24, 3)), pred[72:82], pred[82:]) #self.smpl.save_to_obj(os.path.join(self.pred_path, "{}_pred_{:03d}.obj".format(data_type, i))) #print_mesh(os.path.join(self.pred_path, "epoch.{:03d}.{}_gt_{:03d}.obj".format(epoch, data_type, i)), gt[i-1], smpl.faces) print_mesh( os.path.join( self.pred_path, "{}_epoch.{:03d}.pred_{:03d}.obj".format( data_type, epoch + 1, i)), pred, self.smpl.faces) # Store predicted silhouette and the difference between it and the GT silhouette gt_silhouette = (data[i - 1] * 255).astype("uint8").reshape( data.shape[1], data.shape[2]) #cv2.imwrite(os.path.join(self.pred_path, "{}_epoch.{:03d}.gt_silh_{:03d}.png".format(data_type, epoch + 1, i)), gt_silhouette) pred_silhouette = Mesh( pointcloud=pred).render_silhouette(show=False) #cv2.imwrite(os.path.join(self.pred_path, "{}_epoch.{:03d}.pred_silh_{:03d}.png".format(data_type, epoch + 1, i)), pred_silhouette) diff_silh = abs(gt_silhouette - pred_silhouette) #print(diff_silh.shape) #cv2.imshow("Diff silh", diff_silh) #cv2.imwrite(os.path.join(self.pred_path, "{}_epoch.{:03d}.diff_silh_{:03d}.png".format(data_type, epoch + 1, i)), diff_silh.astype("uint8")) silh_comp = np.concatenate( [gt_silhouette, pred_silhouette, diff_silh]) cv2.imwrite( os.path.join( self.pred_path, "{}_epoch.{:03d}.silh_comp_{:03d}.png".format( data_type, epoch + 1, i)), silh_comp.astype("uint8")) if self.gt_pc[data_type] is not None: print_mesh( os.path.join( self.pred_path, "{}_epoch.{:03d}.gt_pc_{:03d}.obj".format( data_type, epoch + 1, i)), self.gt_pc[data_type], self.smpl.faces) print_point_clouds( os.path.join( self.pred_path, "{}_epoch.{:03d}.comparison_{:03d}.obj". format(data_type, epoch + 1, i)), [pred, self.gt_pc[data_type]], [(255, 0, 0), (0, 255, 0)]) if self.visualise: # Show a random sample rand_index = np.random.randint(low=0, high=len(data)) + 1 mesh = Mesh(filepath=os.path.join( self.pred_path, "{}_epoch.{:03d}.pred_{:03d}.obj". format(data_type, epoch + 1, rand_index))) # Show the true silhouette true_silh = data[rand_index - 1] true_silh = true_silh.reshape(true_silh.shape[:-1]) plt.imshow(true_silh, cmap='gray') plt.title("True {} silhouette {:03d}".format( data_type, rand_index)) plt.show() # Show the predicted silhouette and mesh mesh.render_silhouette( title="Predicted {} silhouette {:03d}".format( data_type, rand_index)) diff_silh = cv2.imread( "{}_epoch.{:03d}.diff_silh_{:03d}.png".format( data_type, epoch + 1, rand_index)) cv2.imshow( "Predicted {} silhouette {:03d}".format( data_type, rand_index), diff_silh) try: mesh.render3D() except Exception: pass