Spaces:
Sleeping
Sleeping
Commit ·
1556762
1
Parent(s): 0e7de7e
Upload 28 files
Browse filesFiles changed (28)
- ModelClass.pyAdded+119
- app.py+101-48
- classifier_weights.pthAdded+3
- inputs/Image_102.jpgAdded
- inputs/Image_129.jpgAdded
- inputs/Image_131.jpgAdded
- inputs/Image_18.jpgAdded
- inputs/Image_180.jpgAdded
- inputs/Image_183.jpgAdded
- inputs/Image_202.jpgAdded
- inputs/Image_207.jpgAdded
- inputs/Image_208.jpgAdded
- inputs/Image_209.jpgAdded
- inputs/Image_213.jpgAdded
- inputs/Image_222.jpgAdded
- inputs/Image_267.jpgAdded
- inputs/Image_28.jpgAdded
- inputs/Image_33.jpgAdded
- inputs/Image_35.jpgAdded
- inputs/Image_409.jpgAdded
- inputs/Image_41.jpgAdded
- inputs/Image_416.jpgAdded
- inputs/Image_417.jpgAdded
- inputs/Image_42.jpgAdded
- inputs/Image_516.jpgAdded
- inputs/Image_527.jpgAdded
- inputs/Image_58.jpgAdded
- inputs/Image_7.jpgAdded
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
import torchfrom torch import nn, optimfrom torchvision import transforms, models#from torch_snippets import *#from torch.utils.data import DataLoader, Dataset#from torchsummary import summary
#import seaborn as sns#import matplotlib.pyplot as plt#from sklearn.model_selection import train_test_splitfrom PIL import Image#import numpy as np#import cv2#from glob import glob#import pandas as pdimport numpy as np
#device = 'cuda' if torch.cuda.is_available() else 'cpu'
class ActionClassifier(nn.Module): def __init__(self, ntargets): super().__init__() resnet = models.resnet50(pretrained=True, progress=True) modules = list(resnet.children())[:-1] # delete last layer self.resnet = nn.Sequential(*modules) for param in self.resnet.parameters(): param.requires_grad = False self.fc = nn.Sequential( nn.Flatten(), nn.BatchNorm1d(resnet.fc.in_features), nn.Dropout(0.2), nn.Linear(resnet.fc.in_features, 256), nn.ReLU(), nn.BatchNorm1d(256), nn.Dropout(0.2), nn.Linear(256, ntargets) ) def forward(self, x): x = self.resnet(x) x = self.fc(x) return x
def get_transform(): transform = transforms.Compose([ transforms.Resize([224, 244]), transforms.ToTensor(), # std multiply by 255 to convert img of [0, 255] # to img of [0, 1] transforms.Normalize((0.485, 0.456, 0.406), (0.229*255, 0.224*255, 0.225*255))] ) return transform
def get_model(): model = ActionClassifier(15) model.load_state_dict(torch.load('./classifier_weights.pth', map_location=torch.device('cpu'))) return model
def get_class(index): ind2cat = [ 'calling', 'clapping', 'cycling', 'dancing', 'drinking', 'eating', 'fighting', 'hugging', 'laughing', 'listening_to_music', 'running', 'sitting', 'sleeping', 'texting', 'using_laptop' ] return ind2cat[index]
# img = Image.open('./inputs/Image_102.jpg').convert('RGB')
# #print(transform(img))
# img = transform(img)
# img = img.unsqueeze(dim=0)# print(img.shape)
# model.eval()# with torch.no_grad():# out = model(img)# out = nn.Softmax()(out).squeeze()# print(out.shape)# res = torch.argmax(out) # print(ind2cat[res])
1 unmodified line23456711 unmodified lines1920212223244 unmodified lines2930313233343536373839401 unmodified line42434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293941 unmodified lineimport numpy as npfrom PIL import Imageimport requests# fix sidebar11 unmodified linesmargin: 0.125rem 2rem;background-color: rgb(181 197 227 / 18%) !important;}</style>""", unsafe_allow_html=True)4 unmodified linesheader {visibility: hidden;}</style>"""st.markdown(hide_st_style, unsafe_allow_html=True)# Function to load and predict imagedef predict(image):# Dummy predictionclasses = ['cat', 'dog']1 unmodified lineprediction /= np.sum(prediction)return dict(zip(classes, prediction))# Define app layout#st.set_page_config(page_title='Image Classification App', page_icon=':camera:', layout='wide')st.title('HappyWhale')st.markdown("[](https://wandb.ai/<username>/<project_name>?workspace=user-<username>)")st.markdown('This project aims to identify whales and dolphins by their unique characteristics. It can help researchers understand their behavior, population dynamics, and migration patterns. This project can aid researchers in identifying these marine mammals, providing valuable data for conservation efforts. [[Source Code]](https://kaggle.com/)')# Add file uploaderuploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])# Add test image selectortest_images = {'Cat': 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg','Dog': 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Golde33443.jpg/1200px-Golde33443.jpg','Bird': 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Scarlet_Tanager_-_male_%28cropped%29.jpg/1200px-Scarlet_Tanager_-_male_%28cropped%29.jpg'}test_image = st.selectbox('Or choose a test image', list(test_images.keys()))st.subheader('Selected Image')# Define layout of appleft_column, right_column = st.columns([1, 2.5], gap="medium")with left_column:if uploaded_file is not None:image = Image.open(uploaded_file)st.image(image, use_column_width=True)else:image_url = test_images[test_image]image = Image.open(requests.get(image_url, stream=True).raw)st.image(image, use_column_width=True)if st.button('✨ Get prediction from AI', type='primary'):spacer = st.empty()prediction = predict(image)right_column.subheader('Results')for class_name, class_probability in prediction.items():right_column.write(f'{class_name}: {class_probability:.2%}')right_column.progress(class_probability)# Display a footer with links and creditsst.markdown("---")st.markdown("Built by [Shamim Ahamed](https://your-portfolio-website.com/). Data provided by [Kaggle](https://www.kaggle.com/c/)")#st.markdown("Data provided by [The Feedback Prize - ELLIPSE Corpus Scoring Challenge on Kaggle](https://www.kaggle.com/c/feedbackprize-ellipse-corpus-scoring-challenge)")1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585911 unmodified lines717273747576777879804 unmodified lines858687888990919293941 unmodified line969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471 unmodified lineimport numpy as npfrom PIL import Imageimport requestsimport ModelClassfrom glob import globimport torchimport torch.nn as nn@st.cache_resourcedef load_model():return ModelClass.get_model()@st.cache_datadef get_images():l = glob('./inputs/*')l = {i.split('/')[-1]: i for i in l}return ldef infer(img):image = img.convert('RGB')image = ModelClass.get_transform()(image)image = image.unsqueeze(dim=0)model = load_model()model.eval()with torch.no_grad():out = model(image)out = nn.Softmax()(out).squeeze()return outst.set_page_config(page_title="Whale Identification",page_icon="🧊",layout="centered",initial_sidebar_state="expanded",menu_items={'Get Help': 'https://www.extremelycoolapp.com/help','Report a bug': "https://www.extremelycoolapp.com/bug",'About': """# This is a header. This is an *extremely* cool app!How how are you doin.---I am fine<style></style>"""})# fix sidebar11 unmodified linesmargin: 0.125rem 2rem;background-color: rgb(181 197 227 / 18%) !important;}.css-1y4p8pa {padding: 3rem 1rem 10rem;max-width: 58rem;}</style>""", unsafe_allow_html=True)4 unmodified linesheader {visibility: hidden;}</style>"""#st.markdown(hide_st_style, unsafe_allow_html=True)def predict(image):# Dummy predictionclasses = ['cat', 'dog']1 unmodified lineprediction /= np.sum(prediction)return dict(zip(classes, prediction))def app():st.title('ActionNet')st.markdown("[](https://wandb.ai/<username>/<project_name>?workspace=user-<username>)")st.markdown('This project aims to identify whales and dolphins by their unique characteristics. It can help researchers understand their behavior, population dynamics, and migration patterns. This project can aid researchers in identifying these marine mammals, providing valuable data for conservation efforts. [[Source Code]](https://kaggle.com/)')uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])test_images = get_images()test_image = st.selectbox('Or choose a test image', list(test_images.keys()))st.subheader('Selected Image')left_column, right_column = st.columns([1.5, 2.5], gap="medium")with left_column:if uploaded_file is not None:image = Image.open(uploaded_file)st.image(image, use_column_width=True)else:image_url = test_images[test_image]image = Image.open(image_url)st.image(image, use_column_width=True)if st.button('✨ Get prediction from AI', type='primary'):spacer = st.empty()res = infer(image)res = torch.argmax(res)cname = ModelClass.get_class(res)st.write(f'{cname}')prediction = predict(image)right_column.subheader('Results')for class_name, class_probability in prediction.items():right_column.write(f'{class_name}: {class_probability:.2%}')right_column.progress(class_probability)st.markdown("---")st.markdown("Built by [Shamim Ahamed](https://your-portfolio-website.com/). Data provided by [Kaggle](https://www.kaggle.com/c/)")app()
123version https://git-lfs.github.com/spec/v1oid sha256:ac67f64705d4109573e06c65925367ee1697f4359ebf1a227a20ba69d3e7a1basize 96504105ModelClass.pyAdded+119
app.py+101-48
classifier_weights.pthAdded+3
inputs/Image_102.jpgAdded

inputs/Image_129.jpgAdded

inputs/Image_131.jpgAdded

inputs/Image_18.jpgAdded

inputs/Image_180.jpgAdded

inputs/Image_183.jpgAdded

inputs/Image_202.jpgAdded

inputs/Image_207.jpgAdded

inputs/Image_208.jpgAdded

inputs/Image_209.jpgAdded

inputs/Image_213.jpgAdded

inputs/Image_222.jpgAdded

inputs/Image_267.jpgAdded

inputs/Image_28.jpgAdded

inputs/Image_33.jpgAdded

inputs/Image_35.jpgAdded

inputs/Image_409.jpgAdded

inputs/Image_41.jpgAdded

inputs/Image_416.jpgAdded

inputs/Image_417.jpgAdded

inputs/Image_42.jpgAdded

inputs/Image_516.jpgAdded

inputs/Image_527.jpgAdded

inputs/Image_58.jpgAdded

inputs/Image_7.jpgAdded
