<jupyter_start><jupyter_text>ExampleIn this simple example, we load an image, pre-process it, and classify it with a pretrained EfficientNet.<jupyter_code>import sys
sys.path.append('../../')

import json
from PIL import Image

import torch
from torchvision import transforms

from efficientnet_pytorch import EfficientNet
from efficientnet_pytorch.utils import *
model_name = 'efficientnet-b0'
image_size = EfficientNet.get_image_size(model_name) # 224
image_size
# Open image
import numpy as np
import imageio

file_name = '/Users/yanqixu/Documents/1.0.MasterCDS/project-summer/breast_img/0_L-CC.png'
loaded_image = np.array(imageio.imread(file_name)).astype(np.float32)

loaded_image = np.expand_dims(np.expand_dims(loaded_image, 0), 0).copy()
mammo = torch.Tensor(loaded_image)
# mammo = torch.cat([tensor_batch,tensor_batch],0)
# mammo.shape
# convert python 2D array into 4D torch tensor in N,C,H,W format
loaded_image = np.expand_dims(np.expand_dims(loaded_image, 0), 0).copy()
tensor_batch = torch.Tensor(loaded_image)#.to(device)
# Preprocess image
img = Image.open('img.jpg')
tfms = transforms.Compose([transforms.Resize(image_size), transforms.CenterCrop(image_size), 
                           transforms.ToTensor(),
                           transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),])
img = tfms(img).unsqueeze(0)
img_new = torch.cat([torch.cat([img[:,:,:,:],torch.zeros(1,3,224,256-224)],3),torch.zeros(1,3,256-224,256)],2)
img_new.shape
# Load class names
labels_map = json.load(open('labels_map.txt'))
labels_map = [labels_map[str(i)] for i in range(1000)]
# Classify with EfficientNet
model = EfficientNet.from_pretrained(model_name,in_channels=1)
model.eval()
with torch.no_grad():
    logits = model(img_new)
preds = torch.topk(logits, k=5).indices.squeeze(0).tolist()

print('-----')
for idx in preds:
    label = labels_map[idx]
    prob = torch.softmax(logits, dim=1)[0, idx].item()
    print('{:<75} ({:.2f}%)'.format(label, prob*100))
new_model = EfficientNet.from_name(model_name,in_channels=1,stem_filters=16)
output = new_model.extract_features(mammo)
local_model = EfficientNet.from_name(model_name,in_channels=3)
output2 = local_model.forward(img_new)
blocks_args = [
        'r1_k3_s11_e1_i32_o16_se0.25',
        'r2_k3_s22_e6_i16_o24_se0.25',
        'r2_k5_s22_e6_i24_o40_se0.25',
        'r3_k3_s22_e6_i40_o80_se0.25',
        'r3_k5_s11_e6_i80_o112_se0.25',
        'r4_k5_s22_e6_i112_o192_se0.25',
        'r1_k3_s11_e6_i192_o320_se0.25',
    ]
blocks_args = BlockDecoder.decode(blocks_args)

blocks_args[0]._replace(num_repeat=6)
blocks_args
global_params = GlobalParams(
        width_coefficient=1,
        depth_coefficient=1,
        image_size=1,
        dropout_rate=1,

        num_classes=2,
        batch_norm_momentum=0.99,
        batch_norm_epsilon=1e-3,
        drop_connect_rate=1,
        depth_divisor=8,
        min_depth=None,
        stem_filters = 32,
    )
global_params._replace(num_classes=10)
global_params<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1302048909451421, 'fraction_numerical': 0.06345009914077991, 'mean_word_length': 4.595194085027726, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '522534', 'license': 'Apache-2.0', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1172, 'n_words': 175, 'path': 'examples/simple/example.ipynb', 'repo_name': 'the-yanqi/EfficientNet-PyTorch', 'size': '3026'}