This repository contains a ResNet50 image classifier trained to detect corrosion types.
1import torch, json
2from PIL import Image
3from torchvision import transforms
4import timm
5
6# Load labels
7labels = ['crevice_corrosion', 'erosion_corrosion', 'galvanic_corrosion', 'mic_corrosion', 'no_corrosion', 'pitting_corrosion', 'stress_corrosion', 'under_insulation_corrosion', 'uniform_corrosion']
8
9# Create model
10model = timm.create_model('resnet50', pretrained=False, num_classes=len(labels))
11state = torch.load('resnet50-corrosion-classifier-v1.pth', map_location='cpu')
12missing, unexpected = model.load_state_dict(state, strict=False)
13model.eval()
14
15# Preprocess (ImageNet)
16transform = transforms.Compose([
17 transforms.Resize(256, interpolation=transforms.InterpolationMode.BICUBIC),
18 transforms.CenterCrop(224),
19 transforms.ToTensor(),
20 transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
21])
22
23img = Image.open('test.jpg').convert('RGB')
24x = transform(img).unsqueeze(0)
25
26with torch.no_grad():
27 logits = model(x)
28 probs = logits.softmax(dim=1).squeeze().tolist()
29
30idx = int(torch.tensor(probs).argmax())
31print(labels[idx], probs[idx])
If your Space works, you can call it programmatically using the Gradio JS Client from Node:
1import { Client, handle_file } from "@gradio/client";
2
3const app = await Client.connect("jacopo22295/RESNET50-CORROSION_CLASSIFIER_V1"); // your Space id
4const res = await fetch("https://example.com/image.jpg");
5const blob = await res.blob();
6const out = await app.predict("/predict", [handle_file(blob)]);
7console.log(out.data);
If you later want to enable the one-click Inference API, consider exporting to a transformers ImageClassification model (e.g. ResNetForImageClassification) and pushing weights + preprocessor_config.json. This requires a small conversion script.