def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = str_to_point_decimal_mark_float( data["steering_angle"]) # The current throttle of the car throttle = str_to_point_decimal_mark_float(data["throttle"]) # The current speed of the car speed = str_to_point_decimal_mark_float(data["speed"]) # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = image_utils.preprocess(np.asarray(image), image_is_rgb=True) steering_angle = float( model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(float_to_comma_decimal_mark_str(steering_angle), float_to_comma_decimal_mark_str(throttle)) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = preprocess(image_array) image_array = np.asarray(image_array, dtype=np.float32) steering_angle = float(model.predict(image_array[None, :, :, :])[0]) throttle = controller.update(float(speed)) print(steering_angle, throttle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def tv_loss_test(correct, tv_loss): content_image = 'styles_images/tubingen.jpg' image_size = 192 tv_weight = 2e-2 content_img = preprocess(PIL.Image.open(content_image), size=image_size) content_img_var = Variable(content_img.type(dtype)) student_output = tv_loss(content_img_var, tv_weight).data.numpy() error = rel_error(correct, student_output) print('TV Loss Error is {:.3f}'.format(error))
def show_saliency_maps(self, X, y, class_names, model): # Convert X and y from numpy arrays to Torch Tensors X_tensor = torch.cat([preprocess(Image.fromarray(x)) for x in X], dim=0) y_tensor = torch.LongTensor(y) # Compute saliency maps for images in X saliency = self.compute_saliency_maps(X_tensor, y_tensor, model) # Convert the saliency map from Torch Tensor to numpy array and show images # and saliency maps together. saliency = saliency.numpy() N = X.shape[0] for i in range(N): plt.subplot(2, N, i + 1) plt.imshow(X[i]) plt.axis('off') plt.title(class_names[y[i]]) plt.subplot(2, N, N + i + 1) plt.imshow(saliency[i], cmap=plt.cm.gray) plt.axis('off') plt.gcf().set_size_inches(12, 5) plt.savefig('visualization/saliency_map.png') plt.show()
import cv2 import numpy as np from tensorflow.keras.models import load_model from image_utils import preprocess model = load_model('./model.h5') print(model.summary()) img = cv2.imread('/home/sajith/Documents/Acedamic/self-driving-car/data/data/IMG/center_2021_01_12_13_20_30_027.jpg') cv2.imshow('tem', img) img = preprocess(img) img = np.asarray(img, dtype=np.float32) print(img.shape) print(float(model.predict(img[None,:,:,:])[0]))
def load_and_preprocess_image(path): """Load an image from a given path and preprocess it.""" image_center = image_utils.load_image(path) return image_utils.preprocess(image_center)
from PIL import Image import matplotlib.pyplot as plt import torchvision import numpy as np from image_utils import preprocess, deprocess from data_utils import load_imagenet_val model = torchvision.models.squeezenet1_1(pretrained=True) X, y, class_names = load_imagenet_val(num=5) idx = 0 target_y = 6 # target label. Change to a different label to see the difference. X_tensor = torch.cat([preprocess(Image.fromarray(x)) for x in X], dim=0) fi = FoolingImage() X_fooling = fi.make_fooling_image(X_tensor[idx:idx + 1], target_y, model) scores = model(Variable(X_fooling)) if target_y == scores.data.max(1)[1][0]: print('Fooled the model!') else: print('The model is not fooled!') X_fooling_np = deprocess(X_fooling.clone()) X_fooling_np = np.asarray(X_fooling_np).astype(np.uint8) plt.subplot(1, 4, 1) plt.imshow(X[idx])