detectron2: throws NotImplementedError: while using pre-trained model - python-3.x

I'm trying to use the pre-trained model of detectron2. While running the following code, it shows NotImplementedError.
import torch
torch.__version__
import torchvision
#torchvision.__version__
!pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.7/index.html
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
import numpy as np
import os, json, cv2, random
import matplotlib.pyplot as plt
%matplotlib inline
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.structures import BoxMode
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCOInstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCOInstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)
And it shows the following error:
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-27-699e754fc9df> in <module>
----> 1 predictor = DefaultPredictor(cfg)
4 frames
/usr/local/lib/python3.8/dist-packages/iopath/common/file_io.py in _isfile(self, path, **kwargs)
438 bool: true if the path is a file
439 """
--> 440 raise NotImplementedError()
441
442 def _isdir(self, path: str, **kwargs: Any) -> bool:
NotImplementedError:

I had the same issue and solved it by manually downloading .pkl file and giving path to cfg.MODEL.WEIGHTS variable
You can try this:
model_weights_url = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
print(f"Downloading model from {model_weights_url}...")
local_model_weights_path = Path("./temp/downloads/model.pkl")
os.makedirs(local_model_weights_path.parent, exist_ok=True)
urllib.request.urlretrieve(model_weights_url, local_model_weights_path)
cfg.MODEL.WEIGHTS = str(local_model_weights_path)

I had the same issue just today. I manually downloaded the R-50.pkl from https://dl.fbaipublicfiles.com/detectron2/ImageNetPretrained/MSRA/R-50.pkl and set cfg.MODEL.WEIGHTS = "R-50.pkl" #path to file

Related

Keras model.fit in Azure ML fails [duplicate]

How to troubleshoot this? I've tried setting dtype=None in the image.img_to_array method.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from keras.preprocessing import image
image_size = (180, 180)
batch_size = 32
model = keras.models.load_model('best_model.h5')
img = keras.preprocessing.image.load_img(
"GarnetCreek_7-15-2019.jpeg", target_size=image_size
)
img_array = image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create batch axis
predictions = model.predict(img_array)
score = predictions[0]
This raises the following error:
Traceback (most recent call last):
img_array = image.img_to_array(img, dtype=None)
return image.img_to_array(img, data_format=data_format, **kwargs)
x = np.asarray(img, dtype=dtype)
return array(a, dtype, copy=False, order=order)
TypeError: __array__() takes 1 positional argument but 2 were given
Has anyone seen this before? Many thanks!
This error sometimes is due to a bug in Pillow 8.3.0 as it is here. (You may not use import PIL directly in your code, however some libraries such as tf.keras.preprocessing.image.load_img use PIL internally)
So, downgrading from PIL 8.3.0 to 8.2.0 may work.
Check PIL version:
import PIL
print(PIL.__version__)
If it is 8.3.0, then you may downgrade to 8.2.0:
!pip install pillow==8.2.0

Getting value error in train.test while eliminating features from dataset using RFE.what is the solution?

valueError image part
Here is the code for eliminating features where I am getting value errors. I want to use recursive feature elimination without specifying any features . I tried to use the RFE(Recursion feature elemination) model to automatically eliminate weak features with each iteration which I have unable to do.HERE is the link of the dataset. https://drive.google.com/file/d/1neYnunu6a_Mdn3NfRZsF8wE4gwMCpjAY/view?usp=sharing .I will be grateful if you suggest me how to do it.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pandas import DataFrame
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.metrics import accuracy_score
from sklearn import datasets
from sklearn.metrics import classification_report
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
df.keys()
x=pd.DataFrame(df)
x.head()
X = df.drop(["Sub_Cat"],axis=1).values
y = df["Sub_Cat"].values
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0)
X_train.shape,X_test.shape
sel=SelectFromModel(RandomForestClassifier(n_estimators=100,random_state=0,n_jobs=-1))
sel.fit(X_train,y_train)
sel.get_support()
[I am getting value error in this part][1]
Then i tried to do this also getting `X = df.drop(["Dst_IP","Timestamp","Flow_ID","Src_IP","Sub_Cat"],axis=1).values
y = df["Sub_Cat"].values
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0)
X = df.drop(["Dst_IP","Timestamp","Flow_ID","Src_IP","Sub_Cat"],axis=1).values
y = df["Sub_Cat"].values
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0)
sel=SelectFromModel(RandomForestClassifier(n_estimators=100,random_state=0,n_jobs=-1))
sel.fit(X_train,y_train)
sel.get_support()
I am still getting error:
ValueError Traceback (most recent call last)
in ()
1 sel=SelectFromModel(RandomForestClassifier(n_estimators=100,random_state=0,n_jobs=-1))
----> 2 sel.fit(X_train,y_train)
3 sel.get_support()
3 frames
/usr/local/lib/python3.7/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
ValueError: could not convert string to float: 'Anomaly'

pytorch customdataset notimplemented error

Im making my own custom datasets in pytorch.
AND, I would like to visualize the image.
however, i think something wrong is happening in custom datasets.
Please help me.
NotImplementedError Traceback (most recent call
last) in ()
1 import matplotlib.pyplot as plt
2 dat= TrainDataset(transforms.ToTensor())
----> 3 img,label= dat[i]
4 plt.imshow(img.permute(1,2,0))
/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py in
getitem(self, index)
31
32 def getitem(self, index) -> T_co:
---> 33 raise NotImplementedError
34
35 def add(self, other: 'Dataset[T_co]') -> 'ConcatDataset[T_co]':
NotImplementedError:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision
from torch.utils.data import DataLoader
import os
import glob
from torch.utils.data import Dataset
import pandas as pd
from PIL import Image
class TrainDataset(Dataset):
def __init__(self, transform):
super().__init__()
self.data = pd.read_csv('/content/drive/MyDrive/cancer/train_labels.csv')
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self,idx):
img_name, label = self.data.iloc[idx]
img = Image.open(f'/content/drive/MyDrive/cancer/test/{image_name}.tif')
img = self.transform(img)
return (img, torch.tensor(label).long())
import matplotlib.pyplot as plt
dat= TrainDataset(transforms.ToTensor())
img,label= dat[1]
plt.imshow(img.permute(1,2,0))

Error 403: Forbidden when trying to load pre-trained model in Google colab

Here is my code to load a pre-trained model in a Google Colab notebookL
# Import resources
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import time
import json
import copy
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import PIL
from PIL import Image
from collections import OrderedDict
import torch
from torch import nn, optim, cuda
from torch.optim import lr_scheduler
from torch.autograd import Variable
import torchvision
from torchvision import datasets, models, transforms
from torch.utils.data.sampler import SubsetRandomSampler
import torch.nn as nn
import torch.nn.functional as F
import os
device = torch.device("cuda" if torch.cuda.is_available()
else "cpu")
print(device)
model = models.resnet152(pretrained=True)
num_in_features = 2048
print(model)
I have run this line before with success, however now I get the error:
Downloading: "https://download.pytorch.org/models/resnet152-b121ed2d.pth" to /root/.cache/torch/checkpoints/resnet152-b121ed2d.pth
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-13-361fc480b515> in <module>()
----> 1 model = models.resnet152(pretrained=True)
2 num_in_features = 2048
3 print(model)
9 frames
/usr/lib/python3.6/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
648 class HTTPDefaultErrorHandler(BaseHandler):
649 def http_error_default(self, req, fp, code, msg, hdrs):
--> 650 raise HTTPError(req.full_url, code, msg, hdrs, fp)
651
652 class HTTPRedirectHandler(BaseHandler):
HTTPError: HTTP Error 403: Forbidden
I have tried loading the model in various other ways, starting a new notebook, and !kill -9 -1
I'm new to Google Colab and am not sure what the issue really is here! Thanks in advance
Looks like this was an issue for lots of people and it is resolved now!
torch.hub._validate_not_a_forked_repo=lambda a,b,c: True
This worked for me, found here.

I'm trying to download the dataset for the kaggle competition twosigma

I have to do a project for my class based on the kaggle competition twosigma but the problem comes when I try to import my dataset from kaggle.competitions.
Can anyone help me?
This is the code I found:
import pandas as pd
import numpy as np
import time
import lightgbm as lgb
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn')
sns.set(font_scale=2)
import warnings
warnings.filterwarnings('ignore')
import os
from kaggle.competitions import twosigmanews
# You can only call make_env() once, so don't lose it!
env = twosigmanews.make_env()
print('Done!')
This is the error I have:
ValueError Traceback (most recent call last)
<ipython-input-4-25118fdba524> in <module>()
----> 1 from kaggle.competitions import twosigmanews
2 # You can only call make_env() once, so don't lose it!
3 env = twosigmanews.make_env()
4 print('Done!')
~\Anaconda3\lib\site-packages\kaggle\__init__.py in <module>()
21
22 api = KaggleApi(ApiClient())
---> 23 api.authenticate()
~\Anaconda3\lib\site-packages\kaggle\api\kaggle_api_extended.py in
authenticate(self)
108
109 # Step 3: load into configuration!
--> 110 self._load_config(config_data)
111
112 def read_config_environment(self, config_data={}, quiet=False):
~\Anaconda3\lib\site-packages\kaggle\api\kaggle_api_extended.py in
_load_config(self, config_data)
147 for item in [self.CONFIG_NAME_USER, self.CONFIG_NAME_KEY]:
148 if item not in config_data:
--> 149 raise ValueError('Error: Missing %s in
configuration.' % item)
150
151 configuration = Configuration()
ValueError: Error: Missing username in configuration.
It seems to be related to the incorrect location of the Kaggle configuration file.
Also, check below related answer:
Error while importing Kaggle dataset on Colab

Resources