def Pnuemonia_detect(): if request.method == 'GET': return render_template('index.html', value='hi') if request.method == 'POST': print(request.files) if 'file' not in request.files: print('file not uploaded') return file = request.files['file'] #image = cv2.imread(file).astype('uint8') image = file.read() #image =np.asarray(file) model = get_model() image = get_tensor(image) output = model.predict(image) if (output == [[0.]]): name = "PNUEMONIA NEGATIVE" else: name = "PNUEMONIA POSITIVE" return render_template('result.html', result=name)
def basic_iterative(model_inp, image_bytes, alpha, epsilon, num_iter, device=torch.device('cpu')): model = get_model(model_inp) tensor = transform_image(image_bytes=image_bytes) #outputs = model.forward(tensor) original_output, perturbed_output, perturbed_image = GAE.basic_iterative( model, tensor, alpha, epsilon, num_iter, device) cpudev = torch.device('cpu') original_output, perturbed_output = original_output.to( cpudev), perturbed_output.to(cpudev) perturbed_image, tensor = perturbed_image.to(cpudev), tensor.to(cpudev) per_tensor = perturbed_image - tensor or_percentage = torch.nn.functional.softmax(original_output, dim=1)[0] * 100 _, or_indices = torch.sort(original_output, descending=True) per_percentage = torch.nn.functional.softmax(perturbed_output, dim=1)[0] * 100 _, per_indices = torch.sort(perturbed_output, descending=True) top5_original = [(imagenet_class_index[str(idx.item())][1], or_percentage[idx.item()].item()) for idx in or_indices[0][:5]] top5_perturbed = [(imagenet_class_index[str(idx.item())][1], per_percentage[idx.item()].item()) for idx in per_indices[0][:5]] perturbed_image = rev_transform(perturbed_image[0]) original_image = rev_transform(tensor[0]) perturbation = rev_transform(per_tensor[0]) del model return dict(top5_original), dict( top5_perturbed), perturbed_image, original_image, perturbation
def show_image(image_name): print("hahahah", image_name) fig = Figure() image = Image.open(UPLOAD_FOLDER + '/' + image_name) model = get_model() top_probability, top_class = predict(image, model, topk=5) with open('categories.json') as f: cat_to_name = json.load(f) fig = view_classify(image, top_probability, top_class, cat_to_name) output = io.BytesIO() FigureCanvasAgg(fig).print_png(output) return Response(output.getvalue(), mimetype="image/png")
def get_prediction(image_bytes): try: model, tokenizer = get_model() inputs = tokenizer([image_bytes], return_tensors='pt') # print('inputs are : ',inputs) reply_ids = model.generate(inputs['input_ids']) print('reply ids : ', reply_ids) replies = ([ tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in reply_ids ]) except Exception: return 0, 'error' return replies
def upload(): if request.method == 'GET': return render_template('index.html', name="Yuhui") if request.method == 'POST': if 'file' not in request.files: flash('file not uploaded , please try again') return f = request.files['file'] image_name = f.filename file_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)) f.save(file_path) request.files.get("image") image = f.read() image = Image.open(file_path) model = get_model() top_probability, top_class = predict(image, model, topk=5) return render_template('result.html', image_name=image_name, top_class=top_class, top_probability=top_probability)
def lbfgs(model_inp, image_bytes, target, c, bin_search_steps, max_iter, const_upper, device=torch.device('cpu')): model = get_model(model_inp) tensor = transform_image(image_bytes=image_bytes) #outputs = model.forward(tensor) target = torch.tensor([target], dtype=torch.long) original_output, perturbed_output, perturbed_image = GAE.lbfgs( model, tensor, target, c, bin_search_steps, max_iter, const_upper, device) cpudev = torch.device('cpu') original_output, perturbed_output = original_output.to( cpudev), perturbed_output.to(cpudev) perturbed_image, tensor = perturbed_image.to(cpudev), tensor.to(cpudev) per_tensor = perturbed_image - tensor or_percentage = torch.nn.functional.softmax(original_output, dim=1)[0] * 100 _, or_indices = torch.sort(original_output, descending=True) per_percentage = torch.nn.functional.softmax(perturbed_output, dim=1)[0] * 100 _, per_indices = torch.sort(perturbed_output, descending=True) top5_original = [(imagenet_class_index[str(idx.item())][1], or_percentage[idx.item()].item()) for idx in or_indices[0][:5]] top5_perturbed = [(imagenet_class_index[str(idx.item())][1], per_percentage[idx.item()].item()) for idx in per_indices[0][:5]] perturbed_image = rev_transform(perturbed_image[0]) original_image = rev_transform(tensor[0]) perturbation = rev_transform(per_tensor[0]) del model return dict(top5_original), dict( top5_perturbed), perturbed_image, original_image, perturbation
import json from commons import get_model, get_tensor import numpy as np encoder = get_model() def get_speaker_embedding(file_path, preprocess=True, sampling_rate=8000, duration=None, normalize=True): ref_audio = get_tensor(file_path, preprocess=preprocess, sampling_rate=sampling_rate, duration=duration) embed, partial_embeds, _ = encoder.embed_utterance(ref_audio, return_partials=True) if (normalize): embed = embed / np.linalg.norm(embed) return embed
import json from commons import get_model, transform_image import torchvision import torchvision.transforms as transforms from PIL import Image import requests from io import BytesIO from torchvision import models import torch import torch.nn as nn import numpy as np import torch.nn.functional as F net = get_model() classes = [ "Adventure", "Comedy", "Action", "Romance", "Drama", "Crime", "Thriller", "Horror", "Mystery", "Documentary" ] device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") def processImage(img): resize = transforms.Resize((224, 224)) normalize = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) to_tensor = transforms.ToTensor() img_resized = resize(Image.fromarray(img)) img_as_tensor = to_tensor(img_resized) return normalize(img_as_tensor)
from commons import get_model, load_image, im_convert import torch import torch.optim as optim from PIL import Image import numpy as np vgg = get_model() def get_prediction(content, style): try: content = load_image(content) style = load_image(style) content_features = get_features(content, vgg) style_features = get_features(style, vgg) style_grams = {layer: gram_matrix(style_features[layer]) for layer in style_features} style_weights = {'conv1_1': 1, 'conv2_1': 0.75, 'conv3_1': 0.2, 'conv4_1': 0.2, 'conv5_1': 0.2} # alpha = content image weight # beta = style image wieght, lower the ratio, the more style is implemented in final image content_weight = 1 style_weight = 1e6 target = content.clone().requires_grad_(True) show_every = 1
from commons import get_tensor, get_model import json with open('cat_to_name.json') as f: cat_to_name = json.load(f) with open('class_to_idx.json') as f: class_to_idx = json.load(f) idx_to_class = {v: k for k, v in class_to_idx.items()} model = get_model('project_checkpoint.pth') def get_flower_name(image_bytes, file): tensor = get_tensor(image_bytes, file) outputs = model.forward(tensor) _, prediction = outputs.max(1) category = prediction.item() print(category) class_idx = idx_to_class[category] flower_name = cat_to_name[class_idx] # file. # save('flowers_1.jpg') flower_name = flower_name[0].upper() + flower_name[1:] return category, flower_name
import json from commons import get_model, transform_image model = get_model('model/densenet121-a639ec97.pth') imagenet_class_index = json.load(open('imagenet_class_index.json')) def get_prediction(image_bytes): try: tensor = transform_image(image_bytes=image_bytes) outputs = model.forward(tensor) except Exception: return 0, 'error' _, y_hat = outputs.max(1) predicted_idx = str(y_hat.item()) return imagenet_class_index[predicted_idx]
with open('cat_to_name.json') as f: cat_to_name = json.load(f) with open('class_to_idx.json') as f: class_to_idx = json.load(f) with open('cuisine_cat_to_name.json') as f: cuisine_cat_to_name = json.load(f) with open('class_to_idx_cuisine.json') as f: class_to_idx_cuisine = json.load(f) idx_to_class = {v: k for k, v in class_to_idx.items()} idx_to_class_cuisine = {v: k for k, v in class_to_idx_cuisine.items()} model = get_model("cash_checkpoint_august_24_2019.pth") model_cuisine = get_model_cuisine("nepali_cuisine_checkpoint_august_23.pth") def get_cuisine_name(image_bytes): tensor = get_tensor(image_bytes) model_cuisine.eval() outputs = model_cuisine.forward(tensor) _, prediction = outputs.max(1) category = prediction.item() class_idx_cuisine = idx_to_class_cuisine[category] cuisine_name = cuisine_cat_to_name[class_idx_cuisine] return category, cuisine_name, outputs def get_cash_name(image_bytes):
import json from commons import get_tensor, get_model with open('cat_to_name.json') as f: cat_to_name = json.load(f) with open('class_to_idx.json') as f: class_to_idx = json.load(f) idx_to_class = {v: k for k, v in class_to_idx.items()} model = get_model("my_checkpoint_densenet_dec_27_2.pth") def get_flower_name(image_bytes): tensor = get_tensor(image_bytes) model.eval() outputs = model.forward(tensor) _, prediction = outputs.max(1) category = prediction.item() class_idx = idx_to_class[category] flower_name = cat_to_name[class_idx] return category, flower_name
import torch from PIL import Image from flask import Flask, request, render_template from commons import get_model from detect import plot_boxes, detect device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = get_model(device) app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def hello_world(): if request.method == 'GET': return render_template('index.html', value='hi') if request.method == 'POST': print(request.files) if 'file' not in request.files: print('file not uploaded') return file = request.files['file'] img_name = file.filename if img_name.endswith('.jpg') or img_name.endswith( '.jpeg') or img_name.endswith('.png'): res_img = 'dect_' + file.filename img = Image.open(file) if img_name.endswith('png'): img = img.convert('RGB')
# Define path to folder 'uploads' in static files APP_ROOT = os.path.dirname(os.path.abspath(__file__)) UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static', 'uploads') application = Flask(__name__) application.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # Set Allowed file extensions application.config["ALLOWED_IMAGE_EXTENSIONS"] = ["JPEG", "JPG", "PNG", "GIF"] # Set Pytorch Home direcotry path to root. Pytorch is using this path to save pretrained models # Prevent permission errors on AWS Beanstalk os.environ['TORCH_HOME'] = './' # Load models at startup VGG16 = Get_VGG16_model() model_transfer = get_model() def allowed_image(filename): ''' This function checks whether a file is an image. It checks whether the file extension is in the 'ALLOWED_IMAGE_EXTENSIONS' Variable Args: filename: path to an image Returns: Returns if file extension indicates an image, else false. ''' # We only want files with a . in the filename if not "." in filename: return False
from commons import get_tensor, get_model import json with open('cat_to_name.json') as f: cat_to_name = json.load(f) model = get_model() def get_class_name(image_bytes): """ Get image bytes from an image, turn them to a tensor and predict the right class for this tensor. :param image_bytes: :return: class's id (int) and class's name (string). """ tensor = get_tensor(image_bytes) outputs = model.forward(tensor) _, prediction = outputs.max(1) pred_class = prediction.item() class_name = cat_to_name[str(pred_class)] return pred_class, class_name
import numpy as np from flask import Flask, request, jsonify, make_response from commons import get_model, get_tensor app = Flask(__name__) model = get_model('weights/weights.pth') @app.route('/predict', methods=['POST']) def predict(): if request.method == 'POST': if 'image' in request.files: try: image = request.files['image'].read() batch = get_tensor(image) except Exception as err: print(err.args) return make_response( jsonify({'Error': 'Upload valid image file'}), 400) output = model(batch) pos = np.mean(output[0].cpu().data.numpy().copy(), axis=0) qtn = np.mean(output[1].cpu().data.numpy().copy(), axis=0) result = {'position': pos.tolist(), 'quaternion': qtn.tolist()} return make_response(jsonify(result), 200) else: return make_response(jsonify({'Error': 'Missed image in request'}), 400) if __name__ == '__main__':