Spaces:
Sleeping
Sleeping
File size: 7,995 Bytes
b53b6d3 4175bb8 b53b6d3 7943976 2b06c57 029ea87 a12be6f 7943976 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f e9be013 521227f 4175bb8 521227f e9be013 4175bb8 f746c21 b53b6d3 521227f b53b6d3 521227f b53b6d3 521227f b53b6d3 2b06c57 0a2fbf3 2b06c57 6461af3 7943976 2b06c57 0a2fbf3 2b06c57 6461af3 2b06c57 7943976 2b06c57 0a2fbf3 7943976 0a2fbf3 2b06c57 0a2fbf3 2b06c57 0a2fbf3 2b06c57 0a2fbf3 2b06c57 f746c21 2b06c57 f746c21 2b06c57 7943976 0a2fbf3 7943976 0a2fbf3 f6c66e9 7943976 0a2fbf3 b53b6d3 0a2fbf3 b53b6d3 f746c21 b53b6d3 029ea87 bc3e686 6461af3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
import cv2
import numpy as np
import gradio as gr
from PIL import Image, ImageOps
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
import os
import time
import io
import base64
import torch
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from functools import partial
class Net2(nn.Module):
def __init__(self):
super(Net2, self).__init__()
self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.pool1 = nn.MaxPool2d(2, 2)
self.dropout1 = nn.Dropout(0.25)
self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(2, 2)
self.dropout2 = nn.Dropout(0.25)
self.conv3 = nn.Conv2d(64, 64, 3, padding=1)
self.bn3 = nn.BatchNorm2d(64)
self.pool3 = nn.MaxPool2d(2, 2)
self.dropout3 = nn.Dropout(0.25)
self.conv4 = nn.Conv2d(64, 64, 3, padding=1)
self.bn4 = nn.BatchNorm2d(64)
self.pool4 = nn.MaxPool2d(2, 2)
self.dropout4 = nn.Dropout(0.25)
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(64 * 5 * 5, 200)
self.fc2 = nn.Linear(200, 150)
self.fc3 = nn.Linear(150, 2)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = self.pool1(x)
x = self.dropout1(x)
x = F.relu(self.bn2(self.conv2(x)))
x = self.pool2(x)
x = self.dropout2(x)
x = F.relu(self.bn3(self.conv3(x)))
x = self.pool3(x)
x = self.dropout3(x)
x = F.relu(self.bn4(self.conv4(x)))
x = self.pool4(x)
x = self.dropout4(x)
x = self.flatten(x)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.softmax(self.fc3(x), dim=1)
return x
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 512, 3, padding=1)
self.bn1 = nn.BatchNorm2d(512)
self.pool1 = nn.MaxPool2d(2, 2)
self.dropout1 = nn.Dropout(0.25)
self.conv2 = nn.Conv2d(512, 256, 3, padding=1)
self.bn2 = nn.BatchNorm2d(256)
self.pool2 = nn.MaxPool2d(2, 2)
self.dropout2 = nn.Dropout(0.25)
self.conv3 = nn.Conv2d(256, 128, 3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.pool3 = nn.MaxPool2d(2, 2)
self.dropout3 = nn.Dropout(0.25)
self.conv4 = nn.Conv2d(128, 64, 3, padding=1)
self.bn4 = nn.BatchNorm2d(64)
self.pool4 = nn.MaxPool2d(2, 2)
self.dropout4 = nn.Dropout(0.20)
self.flatten = nn.Flatten()
self.fc1 = nn.Linear(1600, 300)
self.fc2 = nn.Linear(300, 150)
self.fc3 = nn.Linear(150, 2)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = self.pool1(x)
x = self.dropout1(x)
x = F.relu(self.bn2(self.conv2(x)))
x = self.pool2(x)
x = self.dropout2(x)
x = F.relu(self.bn3(self.conv3(x)))
x = self.pool3(x)
x = self.dropout3(x)
x = F.relu(self.bn4(self.conv4(x)))
x = self.pool4(x)
x = self.dropout4(x)
x = self.flatten(x)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.softmax(self.fc3(x), dim=1)
return x
model = None
model_path = "models1.pth"
# model2 = None
# model2_path = "model4.pth"
if os.path.exists(model_path):
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
new_state_dict = {}
for key, value in state_dict.items():
new_key = key.replace("module.", "")
new_state_dict[new_key] = value
model = Net()
model.load_state_dict(new_state_dict)
model.eval()
else:
print("Model file not found at", model_path)
# def process_image(input_image):
# image = Image.fromarray(input_image).convert("RGB")
#
# start_time = time.time()
# heatmap = scanmap(np.array(image), model)
# elapsed_time = time.time() - start_time
# heatmap_img = Image.fromarray(np.uint8(plt.cm.hot(heatmap) * 255)).convert('RGB')
#
# heatmap_img = heatmap_img.resize(image.size)
#
# return image, heatmap_img, int(elapsed_time)
#
#
# def scanmap(image_np, model):
# image_np = image_np.astype(np.float32) / 255.0
#
# window_size = (80, 80)
# stride = 10
#
# height, width, channels = image_np.shape
#
# probabilities_map = []
#
# for y in range(0, height - window_size[1] + 1, stride):
# row_probabilities = []
# for x in range(0, width - window_size[0] + 1, stride):
# cropped_window = image_np[y:y + window_size[1], x:x + window_size[0]]
# cropped_window_torch = transforms.ToTensor()(cropped_window).unsqueeze(0)
#
# with torch.no_grad():
# probabilities = model(cropped_window_torch)
#
# row_probabilities.append(probabilities[0, 1].item())
#
# probabilities_map.append(row_probabilities)
#
# probabilities_map = np.array(probabilities_map)
# return probabilities_map
#
# def gradio_process_image(input_image):
# original, heatmap, elapsed_time = process_image(input_image)
# return original, heatmap, f"Elapsed Time (seconds): {elapsed_time}"
#
# inputs = gr.Image(label="Upload Image")
# outputs = [
# gr.Image(label="Original Image"),
# gr.Image(label="Heatmap"),
# gr.Textbox(label="Elapsed Time")
# ]
#
# iface = gr.Interface(fn=gradio_process_image, inputs=inputs, outputs=outputs)
# iface.launch()
def scanmap(image_path, model, device, threshold=0.5):
satellite_image = cv2.imread(image_path)
satellite_image = satellite_image.astype(np.float32) / 255.0
window_size = (80, 80)
stride = 10
height, width, channels = satellite_image.shape
fig, ax = plt.subplots(1)
ax.imshow(satellite_image)
ship_images = []
for y in range(0, height - window_size[1] + 1, stride):
for x in range(0, width - window_size[0] + 1, stride):
cropped_window = satellite_image[y:y + window_size[1], x:x + window_size[0]]
cropped_window_torch = torch.tensor(cropped_window.transpose(2, 0, 1), dtype=torch.float32).unsqueeze(0)
cropped_window_torch = cropped_window_torch.to(device) # move data to the same device as model
with torch.no_grad():
probabilities = model(cropped_window_torch)
# if probability is greater than threshold, draw a bounding box and add to ship_images
if probabilities[0, 1].item() > threshold:
rect = patches.Rectangle((x, y), window_size[0], window_size[1], linewidth=1, edgecolor='r',
facecolor='none')
ax.add_patch(rect)
ship_images.append(cropped_window)
output_path = "output.png"
plt.savefig(output_path)
plt.close()
return output_path
def process_image(input_image, model, threshold=0.5):
start_time = time.time()
ship_images = scanmap(input_image, model, threshold)
elapsed_time = time.time() - start_time
return ship_images, int(elapsed_time)
def gradio_process_image(input_image_path, model, threshold=0.5):
start_time = time.time()
output_image_path = scanmap(input_image_path, model, threshold)
elapsed_time = time.time() - start_time
output_image = Image.open(output_image_path) if output_image_path else None
return output_image, f"Elapsed Time (seconds): {elapsed_time}"
inputs = gr.inputs.Image(label="Upload Image")
outputs = [
gr.outputs.Image(label="Detected Ships"),
gr.outputs.Textbox(label="Elapsed Time")
]
# Use 0.5 as the threshold, but adjust according to your needs
gradio_process_image_partial = partial(gradio_process_image, model=model, threshold=0.5)
iface = gr.Interface(fn=gradio_process_image_partial, inputs=inputs, outputs=outputs)
iface.launch()
|