Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
8d25de7b
Unverified
Commit
8d25de7b
authored
Nov 23, 2021
by
Philip Meier
Committed by
GitHub
Nov 23, 2021
Browse files
replace requests with urllib (#4973)
parent
999ef255
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
19 deletions
+8
-19
torchvision/datasets/utils.py
torchvision/datasets/utils.py
+8
-19
No files found.
torchvision/datasets/utils.py
View file @
8d25de7b
...
@@ -183,7 +183,7 @@ def list_files(root: str, suffix: str, prefix: bool = False) -> List[str]:
...
@@ -183,7 +183,7 @@ def list_files(root: str, suffix: str, prefix: bool = False) -> List[str]:
return
files
return
files
def
_quota_exceeded
(
first_chunk
:
bytes
)
->
bool
:
# type: ignore[name-defined]
def
_quota_exceeded
(
first_chunk
:
bytes
)
->
bool
:
try
:
try
:
return
"Google Drive - Quota exceeded"
in
first_chunk
.
decode
()
return
"Google Drive - Quota exceeded"
in
first_chunk
.
decode
()
except
UnicodeDecodeError
:
except
UnicodeDecodeError
:
...
@@ -199,38 +199,28 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[
...
@@ -199,38 +199,28 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[
filename (str, optional): Name to save the file under. If None, use the id of the file.
filename (str, optional): Name to save the file under. If None, use the id of the file.
md5 (str, optional): MD5 checksum of the download. If None, do not check
md5 (str, optional): MD5 checksum of the download. If None, do not check
"""
"""
# Based on https://stackoverflow.com/questions/38511444/python-download-files-from-google-drive-using-url
url
=
f
"https://drive.google.com/uc?export=download&id=
{
file_id
}
"
import
requests
url
=
"https://docs.google.com/uc?export=download"
root
=
os
.
path
.
expanduser
(
root
)
root
=
os
.
path
.
expanduser
(
root
)
if
not
filename
:
if
not
filename
:
filename
=
file_id
filename
=
file_id
fpath
=
os
.
path
.
join
(
root
,
filename
)
fpath
=
os
.
path
.
join
(
root
,
filename
)
os
.
makedirs
(
root
,
exist_ok
=
True
)
if
os
.
path
.
isfile
(
fpath
)
and
check_integrity
(
fpath
,
md5
):
if
os
.
path
.
isfile
(
fpath
)
and
check_integrity
(
fpath
,
md5
):
print
(
"Using downloaded and verified file: "
+
fpath
)
print
(
"Using downloaded and verified file: "
+
fpath
)
else
:
return
session
=
requests
.
Session
()
response
=
session
.
get
(
url
,
params
=
{
"id"
:
file_id
},
stream
=
True
)
token
=
_get_confirm_token
(
response
)
if
token
:
os
.
makedirs
(
root
,
exist_ok
=
True
)
params
=
{
"id"
:
file_id
,
"confirm"
:
token
}
response
=
session
.
get
(
url
,
params
=
params
,
stream
=
True
)
with
urllib
.
request
.
urlopen
(
url
)
as
response
:
# Ideally, one would use response.status_code to check for quota limits, but google drive is not consistent
# Ideally, one would use response.status_code to check for quota limits, but google drive is not consistent
# with their own API, refer https://github.com/pytorch/vision/issues/2992#issuecomment-730614517.
# with their own API, refer https://github.com/pytorch/vision/issues/2992#issuecomment-730614517.
# Should this be fixed at some place in future, one could refactor the following to no longer rely on decoding
# Should this be fixed at some place in future, one could refactor the following to no longer rely on decoding
# the first_chunk of the payload
# the first_chunk of the payload
response_content_generator
=
response
.
iter_content
(
32768
)
content
=
iter
(
lambda
:
response
.
read
(
32768
)
,
b
""
)
first_chunk
=
None
first_chunk
=
None
while
not
first_chunk
:
# filter out keep-alive new chunks
while
not
first_chunk
:
# filter out keep-alive new chunks
first_chunk
=
next
(
response_content_generator
)
first_chunk
=
next
(
content
)
if
_quota_exceeded
(
first_chunk
):
if
_quota_exceeded
(
first_chunk
):
msg
=
(
msg
=
(
...
@@ -240,8 +230,7 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[
...
@@ -240,8 +230,7 @@ def download_file_from_google_drive(file_id: str, root: str, filename: Optional[
)
)
raise
RuntimeError
(
msg
)
raise
RuntimeError
(
msg
)
_save_response_content
(
itertools
.
chain
((
first_chunk
,),
response_content_generator
),
fpath
)
_save_response_content
(
itertools
.
chain
((
first_chunk
,),
content
),
fpath
)
response
.
close
()
def
_get_confirm_token
(
response
:
"requests.models.Response"
)
->
Optional
[
str
]:
# type: ignore[name-defined]
def
_get_confirm_token
(
response
:
"requests.models.Response"
)
->
Optional
[
str
]:
# type: ignore[name-defined]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment